package com.dhcc.sds.busi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dhcc.cdc.busi.entity.CdcEpisodeEntity;
import com.dhcc.cdc.busi.service.ICdcEpisodeService;

import com.dhcc.cdc.sys.entity.DictionaryEntity;
import com.dhcc.cdc.sys.service.IDictionaryService;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.cache.formitem.FormItemCache;
import com.dhcc.sds.base.dao.SdsIndexFieldMapper;
import com.dhcc.sds.base.dao.SdsQcformItemMapper;
import com.dhcc.sds.base.entity.*;
import com.dhcc.sds.base.service.*;
import com.dhcc.sds.busi.dao.AnalysisSdsMstaMapper;
import com.dhcc.sds.busi.dao.SdsQcfitemExecresultMapper;
import com.dhcc.sds.busi.dto.*;
import com.dhcc.sds.busi.entity.SdsQcfitemExecresultEntity;
import com.dhcc.sds.busi.entity.SdsQcincaseEntity;
import com.dhcc.sds.busi.service.IAnalysisSdsIndstaService;
import com.dhcc.sds.busi.service.ISdsQcfitemExecresultService;
import com.dhcc.sds.busi.service.ISdsQcincaseOperlogService;
import com.dhcc.sds.busi.service.ISdsQcincaseService;
import com.dhcc.sds.busi.vo.AnalysisSdsPortalVo;
import com.dhcc.sds.util.DateUtil;
import com.dhcc.sds.util.StrUtil;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.MapContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author 80572
 * @version 1.0
 * @date 2021/8/8 17:36
 */
@Service
public class AnalysisSdsIndstaServiceImpl implements IAnalysisSdsIndstaService {

    @Autowired
    private ISdsQcincaseService sdsQcincaseService;

    @Autowired
    private ISdsQcfitemExecresultService sdsQcfitemExecresultService;

    @Autowired
    private ICdcEpisodeService cdcEpisodeService;

    @Autowired
    private ISdsPrimaryHeaderService sdsPrimaryHeaderService;

    @Autowired
    private ISdsSecondaryHeaderService sdsSecondaryHeaderService;

    @Resource
    private SdsIndexFieldMapper sdsIndexFieldMapper;

    @Autowired
    private ISdsQcfitemDicService sdsQcfitemDicService;

    @Resource
    private SdsQcformItemMapper sdsQcformItemMapper;

    @Autowired
    private IDictionaryService dictionaryService;

    @Autowired
    private ISdsIndexHeaderService sdsIndexHeaderService;

    @Autowired
    private ISdsIndexFieldService sdsIndexFieldService;

    @Resource
    private AnalysisSdsMstaMapper analysisSdsMstaMapper;

    @Autowired
    private ISdsQcincaseOperlogService sdsQcincaseOperlogService;

    @Autowired
    private ISdsCommonService sdsCommonService;
    @Autowired
    private SdsQcfitemExecresultMapper sdsQcfitemExecresultMapper;
    Double cur =(double) 0;

    /**
     * 小括号及其里面内容
     */
    private static String p1 = "\\(.*?\\)";
    /**
     * 中括号及其里面内容
     */
    private static String p2 = "\\[.*?\\]";
    /**
     * 大括号及其里面内容
     */
    private static String p3 = "\\{.*?\\}";

    @Override
    public JSONObject selectSdsIndsta(Map<String, Object> params) {
        JSONObject departDataObj = new JSONObject();
        //1.根据出院时间查询符合的病例
        String month = (String) params.get("month");
        String year = (String) params.get("year");
        if (!StringUtils.isEmpty(month)){
            String month2 = year + "-" + month;
            params.put("month", month2);
        }
        List<STEMIIndexStaDto> stemiIndexStaDtos = sdsQcincaseService.getIndexDatas(params);
        for (STEMIIndexStaDto stemiIndexStaDto : stemiIndexStaDtos){
            String dishDate = stemiIndexStaDto.getDishDate();
            params.put("dishDate", dishDate);
            List<Long> episodeIds = sdsQcincaseService.getIndexNums(params);
            List<AnalysisCalDto> analysisCalDtos = new ArrayList<>();
            for (Long l : episodeIds){
                AnalysisCalDto analysisCalDto = new AnalysisCalDto();
                params.put("episodeId", l);
                SdsQcincaseEntity sdsQcincaseEntity = sdsQcincaseService.selectByEpisodeId(params);
                Long inCaseId = sdsQcincaseEntity.getId();
                STEMIIndexStaDto stemiIndexStaDto1 = new STEMIIndexStaDto();
                //指标id
                //入院时间
                Long formID111 = 1392751506108818792l;
                String STEMI111 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID111, inCaseId);
                Date admDate = DateUtil.parseTime(STEMI111);
                Long formID1 = 1392751506108818811l;
                String STEMI1 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID1, inCaseId);
                if (!StringUtils.isEmpty(STEMI1)){
                    Date stemi1Date = DateUtil.parseTime(STEMI1);
                    Long m = DateUtil.getDateCompare(stemi1Date, admDate, "m");
                    //首剂双联抗血小板药物使用情况
                    analysisCalDto.setIndex26(m);
                }
                /*String s1 = Long.toString(m);
                  stemiIndexStaDto1.setSTEMI1(s1);*/
                Long formID21 = 1392751506108818824l;
                String STEMI21 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID21, inCaseId);
                /*stemiIndexStaDto1.setSTEMI21(STEMI21);*/
                //实施首次超声心动图(CDFA)检查占比
                analysisCalDto.setIndex1(StrUtil.getSdsFormValue(STEMI21));
                Long formID22 = 1392751506108818821l;
                String STEMI22 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID22, inCaseId);
                /*stemiIndexStaDto1.setSTEMI22(STEMI22);*/
                //实施首次X线胸片检查占比
                analysisCalDto.setIndex2(StrUtil.getSdsFormValue(STEMI22));
                Long formID23 = 1392751506108818829l;
                String STEMI23 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID23, inCaseId);
                /*stemiIndexStaDto1.setSTEMI23(STEMI23);*/
                //完成 GRACE 评估的比率
                analysisCalDto.setIndex3(StrUtil.getSdsFormValue(STEMI23));
                Long formID24 = 1392751506108818833l;
                String STEMI24 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID24, inCaseId);
                /*stemiIndexStaDto1.setSTEMI24(STEMI24);*/
                //完成 CRUSADE 评估的比率
                analysisCalDto.setIndex4(StrUtil.getSdsFormValue(STEMI24));
                //到达医院STEMI确诊报告日期与时间至溶栓时间
                Long formID31 = 1392751506108818841l;
                String STEMI31 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID31, inCaseId);
                Long formID32 = 1392751506108818803l;
                String STEMI32 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID32, inCaseId);
                if (!StringUtils.isEmpty(STEMI32) && !StringUtils.isEmpty(STEMI31)){
                    Long m2 = DateUtil.getDateCompare(DateUtil.parseTime(STEMI31), DateUtil.parseTime(STEMI32), "m");
                    analysisCalDto.setIndex27(m2);
                }
                /*Long m2 = DateUtil.getDateCompare(DateUtil.parseTime(STEMI31), DateUtil.parseTime(STEMI32), "m");
                  String s2 = Long.toString(m2);
                String s2 = "";
                stemiIndexStaDto1.setSTEMI31(s2);*/
                //todo 确诊STEMI至PCI导丝通过梗死相关动脉（D2B）时间没录进去
                /*Long formID32 = 1392751506108818821l;
                  String STEMI32 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID32, inCaseId);
                  stemiIndexStaDto.setSTEMI32(STEMI32);*/
                Long formID4 = 1392751506108818917l;
                String STEMI4 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID4, inCaseId);
                String s4 = "n";
                if (Objects.equals("n", STEMI4)){
                    s4 = "y";
                }
                /*stemiIndexStaDto1.setSTEMI4(s4);*/
                //使用首剂β-受体阻滞剂占比
                analysisCalDto.setIndex5(StrUtil.getSdsFormValue(s4));
                Long formID51 = 1392751506108819022l;
                String STEMI51 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID51, inCaseId);
                /*stemiIndexStaDto1.setSTEMI51(STEMI51);*/
                //住院期间双联抗血小板药物使用占比
                analysisCalDto.setIndex6(StrUtil.getSdsFormValue(STEMI51));
                Long formID52 = 1392751506108819025l;
                String STEMI52 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID52, inCaseId);
                /*stemiIndexStaDto1.setSTEMI52(STEMI52);*/
                //住院期间β受体阻滞剂使用占比
                analysisCalDto.setIndex7(StrUtil.getSdsFormValue(STEMI52));
                Long formID53 = 1392751506108819029l;
                String STEMI53 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID53, inCaseId);
                /*stemiIndexStaDto1.setSTEMI53(STEMI53);*/
                //住院期间ACEI或ARB使用占比
                analysisCalDto.setIndex8(StrUtil.getSdsFormValue(STEMI53));
                Long formID54 = 1392751506108818919l;
                String STEMI54 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID54, inCaseId);
                /*stemiIndexStaDto1.setSTEMI54(STEMI54);*/
                //住院期间他汀类药物使用占比
                analysisCalDto.setIndex9(StrUtil.getSdsFormValue(STEMI54));
                Long formID61 = 1392751506108818922l;
                String STEMI61 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID61, inCaseId);
                /*stemiIndexStaDto1.setSTEMI61(STEMI61);*/
                //出院时双联抗血小板药物使用占比
                analysisCalDto.setIndex10(StrUtil.getSdsFormValue(STEMI61));
                Long formID62 = 1392751506108818933l;
                String STEMI62 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID62, inCaseId);
                /*stemiIndexStaDto1.setSTEMI62(STEMI62);*/
                //出院时β受体阻滞剂使用占比
                analysisCalDto.setIndex11(StrUtil.getSdsFormValue(STEMI62));
                Long formID63 = 1392751506108818927l;
                String STEMI63 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID63, inCaseId);
                /*stemiIndexStaDto1.setSTEMI63(STEMI63);*/
                //出院时ACEI或ARB使用占比
                analysisCalDto.setIndex12(StrUtil.getSdsFormValue(STEMI63));
                Long formID64 = 1392751506108818933l;
                String STEMI64 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID64, inCaseId);
                /*stemiIndexStaDto1.setSTEMI64(STEMI64);*/
                //出院时他汀类药物使用占比
                analysisCalDto.setIndex13(StrUtil.getSdsFormValue(STEMI64));
                Long formID65 = 1392751506108818941l;
                String STEMI65 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID65, inCaseId);
                /*stemiIndexStaDto1.setSTEMI65(STEMI65);*/
                //出院时醛固酮受体拮抗剂使用占比
                analysisCalDto.setIndex14(StrUtil.getSdsFormValue(STEMI65));
                Long formID112 = 1392751506108818793l;
                String STEMI112 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID112, inCaseId);
                Long d = DateUtil.getDateCompare(DateUtil.parseTime(STEMI112), DateUtil.parseTime(STEMI111), "d");
                /*String s11 = Long.toString(d);
                stemiIndexStaDto1.setSTEMI11(s11);*/
                //平均住院日
                analysisCalDto.setIndex29(d);
                Long formID7 = 1392751506108818963l;
                String STEMI7 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID7, inCaseId);
                /*stemiIndexStaDto1.setSTEMI7(STEMI7);*/
                //平均住院日费用
                analysisCalDto.setIndex21(Double.parseDouble(STEMI7));
                Long formID8 = 1392751506108818958l;
                String STEMI8 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID8, inCaseId);
                /*stemiIndexStaDto1.setSTEMI8(STEMI8);*/
                //西药费
                analysisCalDto.setIndex22(Double.parseDouble(STEMI8));
                //todo 手术费未录入
                /*Long formID9 = 1392751506108818811l;
                  String STEMI9 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID9, inCaseId);
                  stemiIndexStaDto.setSTEMI9(STEMI9);*/
                //todo 平均手术用一次性医用材料费用
                /*Long formID10 = 1392751506108818811l;
                  String STEMI10 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID10, inCaseId);
                  stemiIndexStaDto.setSTEMI10(STEMI10);*/
                analysisCalDtos.add(analysisCalDto);
            }
            long s1 = 0l;
            int s21 = 0;
            int s22 = 0;
            int s23 = 0;
            int s24 = 0;
            long s31 = 0l;
            int s4 = 0;
            int s51 = 0;
            int s52 = 0;
            int s53 = 0;
            int s54 = 0;
            int s61 = 0;
            int s62 = 0;
            int s63 = 0;
            int s64 = 0;
            int s65 = 0;
            long s11 = 0l;
            double s7 = 0;
            double s8 = 0;
            for (int i = 0; i < analysisCalDtos.size(); i++){
                s1 += analysisCalDtos.get(i).getIndex26();
                s21 += analysisCalDtos.get(i).getIndex1();
                s22 += analysisCalDtos.get(i).getIndex2();
                s23 += analysisCalDtos.get(i).getIndex3();
                s24 += analysisCalDtos.get(i).getIndex4();
                //s31 += analysisCalDtos.get(i).getIndex27();
                s4 += analysisCalDtos.get(i).getIndex5();
                s51 += analysisCalDtos.get(i).getIndex6();
                s52 += analysisCalDtos.get(i).getIndex7();
                s53 += analysisCalDtos.get(i).getIndex8();
                s54 += analysisCalDtos.get(i).getIndex9();
                s61 += analysisCalDtos.get(i).getIndex10();
                s62 += analysisCalDtos.get(i).getIndex11();
                s63 += analysisCalDtos.get(i).getIndex12();
                s64 += analysisCalDtos.get(i).getIndex13();
                s65 += analysisCalDtos.get(i).getIndex14();
                s11 += analysisCalDtos.get(i).getIndex29();
                s7 += analysisCalDtos.get(i).getIndex21();
                s8 += analysisCalDtos.get(i).getIndex22();
            }
            stemiIndexStaDto.setSTEMI1(StrUtil.deciFormatLong(s1, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMI21(StrUtil.deciFormat(s21, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum21(s21);
            stemiIndexStaDto.setSTEMI22(StrUtil.deciFormat(s22, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum22(s22);
            stemiIndexStaDto.setSTEMI23(StrUtil.deciFormat(s23, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum23(s23);
            stemiIndexStaDto.setSTEMI24(StrUtil.deciFormat(s24, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum24(s24);
            //stemiIndexStaDto.setSTEMI31(StrUtil.deciFormatLong(s31, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMI4(StrUtil.deciFormat(s4, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum4(s4);
            stemiIndexStaDto.setSTEMI51(StrUtil.deciFormat(s51, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum51(s51);
            stemiIndexStaDto.setSTEMI52(StrUtil.deciFormat(s52, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum52(s52);
            stemiIndexStaDto.setSTEMI53(StrUtil.deciFormat(s53, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum53(s53);
            stemiIndexStaDto.setSTEMI54(StrUtil.deciFormat(s54, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum54(s54);
            stemiIndexStaDto.setSTEMI61(StrUtil.deciFormat(s61, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum61(s61);
            stemiIndexStaDto.setSTEMI62(StrUtil.deciFormat(s62, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum62(s62);
            stemiIndexStaDto.setSTEMI63(StrUtil.deciFormat(s63, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum63(s63);
            stemiIndexStaDto.setSTEMI64(StrUtil.deciFormat(s64, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum64(s64);
            stemiIndexStaDto.setSTEMI65(StrUtil.deciFormat(s65, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMINum65(s65);
            stemiIndexStaDto.setSTEMI11(StrUtil.deciFormatLong(s11, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMI7(StrUtil.deciFormatDouble(s7, analysisCalDtos.size()));
            stemiIndexStaDto.setSTEMI8(StrUtil.deciFormatDouble(s8, analysisCalDtos.size()));
        }

        /*List<SdsQcincaseEntity> sdsQcincaseEntities = new ArrayList<>();*/
       /* for (SdsQcincaseEntity sdsQcincaseEntity : sdsQcincaseEntities){
            Long inCaseId = sdsQcincaseEntity.getId();
            Long episodeId = sdsQcincaseEntity.getEpisodeId();
            STEMIIndexStaDto stemiIndexStaDto = new STEMIIndexStaDto();
            String name = cdcEpisodeService.getNameByEpisodeId(episodeId);
            *//*stemiIndexStaDto.setName(name);*//*
            //指标id
            //入院时间
            Long formID111 = 1392751506108818792l;
            String STEMI111 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID111, inCaseId);
            Date admDate = DateUtil.parseTime(STEMI111);
            Long formID1 = 1392751506108818811l;
            String STEMI1 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID1, inCaseId);
            Date stemi1Date = DateUtil.parseTime(STEMI1);
            Long m = DateUtil.getDateCompare(stemi1Date, admDate, "m");
            String s1 = Long.toString(m);
            stemiIndexStaDto.setSTEMI1(s1);
            Long formID21 = 1392751506108818824l;
            String STEMI21 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID21, inCaseId);
            stemiIndexStaDto.setSTEMI21(STEMI21);
            Long formID22 = 1392751506108818821l;
            String STEMI22 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID22, inCaseId);
            stemiIndexStaDto.setSTEMI22(STEMI22);
            Long formID23 = 1392751506108818829l;
            String STEMI23 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID23, inCaseId);
            stemiIndexStaDto.setSTEMI23(STEMI23);
            Long formID24 = 1392751506108818833l;
            String STEMI24 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID24, inCaseId);
            stemiIndexStaDto.setSTEMI24(STEMI24);
            //到达医院STEMI确诊报告日期与时间至溶栓时间
            Long formID31 = 1392751506108818841l;
            String STEMI31 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID31, inCaseId);
            Long formID32 = 1392751506108818803l;
            String STEMI32 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID32, inCaseId);
            *//*Long m2 = DateUtil.getDateCompare(DateUtil.parseTime(STEMI31), DateUtil.parseTime(STEMI32), "m");
            String s2 = Long.toString(m2);*//*
            String s2 = "";
            stemiIndexStaDto.setSTEMI31(s2);
            //todo 这条没录进去
            *//*Long formID32 = 1392751506108818821l;
            String STEMI32 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID32, inCaseId);
            stemiIndexStaDto.setSTEMI32(STEMI32);*//*
            Long formID4 = 1392751506108818917l;
            String STEMI4 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID4, inCaseId);
            String s4 = "n";
            if (!StringUtils.isEmpty(STEMI4)){
                s4 = "y";
            }
            stemiIndexStaDto.setSTEMI4(s4);
            Long formID51 = 1392751506108819022l;
            String STEMI51 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID51, inCaseId);
            stemiIndexStaDto.setSTEMI51(STEMI51);
            Long formID52 = 1392751506108819025l;
            String STEMI52 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID52, inCaseId);
            stemiIndexStaDto.setSTEMI52(STEMI52);
            Long formID53 = 1392751506108819029l;
            String STEMI53 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID53, inCaseId);
            stemiIndexStaDto.setSTEMI53(STEMI53);
            Long formID54 = 1392751506108818919l;
            String STEMI54 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID54, inCaseId);
            stemiIndexStaDto.setSTEMI54(STEMI54);
            Long formID61 = 1392751506108818922l;
            String STEMI61 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID61, inCaseId);
            stemiIndexStaDto.setSTEMI61(STEMI61);
            Long formID62 = 1392751506108818933l;
            String STEMI62 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID62, inCaseId);
            stemiIndexStaDto.setSTEMI62(STEMI62);
            Long formID63 = 1392751506108818927l;
            String STEMI63 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID63, inCaseId);
            stemiIndexStaDto.setSTEMI63(STEMI63);
            Long formID64 = 1392751506108818933l;
            String STEMI64 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID64, inCaseId);
            stemiIndexStaDto.setSTEMI64(STEMI64);
            Long formID65 = 1392751506108818941l;
            String STEMI65 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID65, inCaseId);
            stemiIndexStaDto.setSTEMI65(STEMI65);

            Long formID112 = 1392751506108818793l;
            String STEMI112 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID112, inCaseId);
            //todo 出院-入院
            Long d = DateUtil.getDateCompare(DateUtil.parseTime(STEMI112), DateUtil.parseTime(STEMI111), "d");
            String s11 = Long.toString(d);
            stemiIndexStaDto.setSTEMI11(s11);
            Long formID7 = 1392751506108818963l;
            String STEMI7 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID7, inCaseId);
            stemiIndexStaDto.setSTEMI7(STEMI7);
            //西药费
            Long formID8 = 1392751506108818958l;
            String STEMI8 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID8, inCaseId);
            stemiIndexStaDto.setSTEMI8(STEMI8);
            //todo 手术费未录入
            *//*Long formID9 = 1392751506108818811l;
            String STEMI9 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID9, inCaseId);
            stemiIndexStaDto.setSTEMI9(STEMI9);*//*
            //todo 平均手术用一次性医用材料费用
            *//*Long formID10 = 1392751506108818811l;
            String STEMI10 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID10, inCaseId);
            stemiIndexStaDto.setSTEMI10(STEMI10);*//*


            statisList.add(stemiIndexStaDto);
        }*/


        /*confirmRate.add(splitJSONObj("首剂双联抗血小板药物用药延迟", StrUtil.deciFormatU(1, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("实施首次CDFA检查", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("实施首次X线胸片检查", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("完成GRACE评估", StrUtil.deciFormatU(2, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("完成CRUSADE评估", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("STEMI确诊报告至溶栓时间", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("STEMI至PCI导丝通过梗死相关动脉时间", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("首剂β-受体阻滞剂", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("住院期间双联抗血小板药物", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("住院期间β受体阻滞剂", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("住院期间ACEI或ARB", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("住院期间他汀类药物", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("出院时β受体阻滞剂", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("出院时双联抗血小板药物", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("出院时ACEI或ARB", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("出院时他汀类药物", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("出院时醛固酮受体拮抗剂", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("平均住院日费用", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("平均药费", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("平均手术治疗费用", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("平均手术用一次性医用材料费用", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("平均住院日", StrUtil.deciFormatU(3, 4), "confirmRate"));*/
        JSONArray chartDatas = new JSONArray();
        JSONArray confirmRate = new JSONArray();
        confirmRate.add(splitJSONObj("2021-01", 0, "upNum"));
        confirmRate.add(splitJSONObj("2021-03", 10, "upNum"));
        confirmRate.add(splitJSONObj("2021-05", 25, "upNum"));
        chartDatas.add(confirmRate);
        departDataObj.put("chartDatas", chartDatas);

        JSONArray chartDatas2 = new JSONArray();
        JSONArray confirmRate2 = new JSONArray();
        confirmRate2.add(splitJSONObj("2021-01", 0, "confirmRate2"));
        confirmRate2.add(splitJSONObj("2021-03", 60, "confirmRate2"));
        confirmRate2.add(splitJSONObj("2021-05", 100, "confirmRate2"));
        chartDatas2.add(confirmRate2);
        departDataObj.put("chartDatas2", chartDatas2);

        JSONArray chartDatas3 = new JSONArray();
        JSONArray confirmRate3 = new JSONArray();
        confirmRate3.add(splitJSONObj("2021-01", 0, "confirmRate3"));
        confirmRate3.add(splitJSONObj("2021-03", 80, "confirmRate3"));
        confirmRate3.add(splitJSONObj("2021-05", 200, "confirmRate3"));
        chartDatas3.add(confirmRate3);
        departDataObj.put("chartDatas3", chartDatas3);

        departDataObj.put("tableDates", stemiIndexStaDtos);
        return departDataObj;
    }

    @Override
    public JSONObject selectSdsIndstaDetail(Map<String, Object> params) {
        JSONObject departDataObj = new JSONObject();

        List<STEMIIndexStaDto> statisList = new ArrayList<>();

        List<Long> episodeIds = sdsQcincaseService.getIndexNums(params);
        for (Long l : episodeIds){
            STEMIIndexStaDto stemiIndexStaDto = new STEMIIndexStaDto();
            params.put("episodeId", l);
            SdsQcincaseEntity sdsQcincaseEntity = sdsQcincaseService.selectByEpisodeId(params);
            Long inCaseId = sdsQcincaseEntity.getId();
            String name = cdcEpisodeService.getNameByEpisodeId(l);
            stemiIndexStaDto.setName(name);
            //指标id
            //入院时间
            Long formID111 = 1392751506108818792l;
            String STEMI111 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID111, inCaseId);
            Date admDate = DateUtil.parseTime(STEMI111);
            Long formID1 = 1392751506108818811l;
            String STEMI1 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID1, inCaseId);
            if (!StringUtils.isEmpty(STEMI1)){
                Date stemi1Date = DateUtil.parseTime(STEMI1);
                Long m = DateUtil.getDateCompare(stemi1Date, admDate, "m");
                String s1 = Long.toString(m);
                stemiIndexStaDto.setSTEMI1(s1);
            }
            Long formID21 = 1392751506108818824l;
            String STEMI21 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID21, inCaseId);
            stemiIndexStaDto.setSTEMI21(STEMI21);
            Long formID22 = 1392751506108818821l;
            String STEMI22 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID22, inCaseId);
            stemiIndexStaDto.setSTEMI22(STEMI22);
            Long formID23 = 1392751506108818829l;
            String STEMI23 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID23, inCaseId);
            stemiIndexStaDto.setSTEMI23(STEMI23);
            Long formID24 = 1392751506108818833l;
            String STEMI24 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID24, inCaseId);
            stemiIndexStaDto.setSTEMI24(STEMI24);
            Long formID31 = 1392751506108818841l;
            String STEMI31 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID31, inCaseId);
            Long formID32 = 1392751506108818803l;
            String STEMI32 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID32, inCaseId);
            if (!StringUtils.isEmpty(STEMI32) && !StringUtils.isEmpty(STEMI31)){
                Long m2 = DateUtil.getDateCompare(DateUtil.parseTime(STEMI31), DateUtil.parseTime(STEMI32), "m");
                String s2 = Long.toString(m2);
                stemiIndexStaDto.setSTEMI31(s2);
            }
            //todo 确诊STEMI至PCI导丝通过梗死相关动脉（D2B）时间没录进去
            Long formID4 = 1392751506108818917l;
            String STEMI4 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID4, inCaseId);
            String s4 = "n";
            if (Objects.equals("n", STEMI4)){
                s4 = "y";
            }
            stemiIndexStaDto.setSTEMI4(s4);
            Long formID51 = 1392751506108819022l;
            String STEMI51 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID51, inCaseId);
            stemiIndexStaDto.setSTEMI51(STEMI51);
            Long formID52 = 1392751506108819025l;
            String STEMI52 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID52, inCaseId);
            stemiIndexStaDto.setSTEMI52(STEMI52);
            Long formID53 = 1392751506108819029l;
            String STEMI53 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID53, inCaseId);
            stemiIndexStaDto.setSTEMI53(STEMI53);
            Long formID54 = 1392751506108818919l;
            String STEMI54 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID54, inCaseId);
            stemiIndexStaDto.setSTEMI54(STEMI54);
            Long formID61 = 1392751506108818922l;
            String STEMI61 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID61, inCaseId);
            stemiIndexStaDto.setSTEMI61(STEMI61);
            Long formID62 = 1392751506108818933l;
            String STEMI62 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID62, inCaseId);
            stemiIndexStaDto.setSTEMI62(STEMI62);
            Long formID63 = 1392751506108818927l;
            String STEMI63 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID63, inCaseId);
            stemiIndexStaDto.setSTEMI63(STEMI63);
            Long formID64 = 1392751506108818933l;
            String STEMI64 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID64, inCaseId);
            stemiIndexStaDto.setSTEMI64(STEMI64);
            Long formID65 = 1392751506108818941l;
            String STEMI65 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID65, inCaseId);
            stemiIndexStaDto.setSTEMI65(STEMI65);
            Long formID112 = 1392751506108818793l;
            String STEMI112 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID112, inCaseId);
            Long d = DateUtil.getDateCompare(DateUtil.parseTime(STEMI112), DateUtil.parseTime(STEMI111), "d");
            String s11 = Long.toString(d);
            stemiIndexStaDto.setSTEMI11(s11);
            Long formID7 = 1392751506108818963l;
            String STEMI7 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID7, inCaseId);
            stemiIndexStaDto.setSTEMI7(STEMI7);
            Long formID8 = 1392751506108818958l;
            String STEMI8 = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(formID8, inCaseId);
            stemiIndexStaDto.setSTEMI8(STEMI8);
            //todo 手术费未录入
            //todo 平均手术用一次性医用材料费用
            statisList.add(stemiIndexStaDto);
        }

        /*JSONArray confirmRate = new JSONArray();
        confirmRate.add(splitJSONObj("首剂双联抗血小板药物用药延迟", StrUtil.deciFormatU(1, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("实施首次CDFA检查", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("实施首次X线胸片检查", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("完成GRACE评估", StrUtil.deciFormatU(2, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("完成CRUSADE评估", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("STEMI确诊报告至溶栓时间", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("STEMI至PCI导丝通过梗死相关动脉时间", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("首剂β-受体阻滞剂", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("住院期间双联抗血小板药物", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("住院期间β受体阻滞剂", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("住院期间ACEI或ARB", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("住院期间他汀类药物", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("出院时β受体阻滞剂", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("出院时双联抗血小板药物", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("出院时ACEI或ARB", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("出院时他汀类药物", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("出院时醛固酮受体拮抗剂", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("平均住院日费用", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("平均药费", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("平均手术治疗费用", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("平均手术用一次性医用材料费用", StrUtil.deciFormatU(3, 4), "confirmRate"));
        confirmRate.add(splitJSONObj("平均住院日", StrUtil.deciFormatU(3, 4), "confirmRate"));*/
        JSONArray chartDatas = new JSONArray();
        JSONArray confirmRate = new JSONArray();
        confirmRate.add(splitJSONObj("05-01", 2, "upNum"));
        confirmRate.add(splitJSONObj("05-11", 18, "upNum"));
        confirmRate.add(splitJSONObj("05-20", 25, "upNum"));
        chartDatas.add(confirmRate);
        departDataObj.put("chartDatas", chartDatas);

        JSONArray chartDatas2 = new JSONArray();
        JSONArray confirmRate2 = new JSONArray();
        confirmRate2.add(splitJSONObj("05-01", 30, "confirmRate2"));
        confirmRate2.add(splitJSONObj("05-11", 100, "confirmRate2"));
        confirmRate2.add(splitJSONObj("05-20", 80, "confirmRate2"));
        chartDatas2.add(confirmRate2);
        departDataObj.put("chartDatas2", chartDatas2);

        JSONArray chartDatas3 = new JSONArray();
        JSONArray confirmRate3 = new JSONArray();
        confirmRate3.add(splitJSONObj("05-01", 20, "confirmRate3"));
        confirmRate3.add(splitJSONObj("05-11", 400, "confirmRate3"));
        confirmRate3.add(splitJSONObj("05-20", 300, "confirmRate3"));
        chartDatas3.add(confirmRate3);
        departDataObj.put("chartDatas3", chartDatas3);

        departDataObj.put("tableDates", statisList);
        return departDataObj;
    }

    @Override
    public JSONObject selectSdsIndColumns(Map<String, Object> params) {
        JSONObject jsonObject = new JSONObject();
        //一级指标
        List<SdsPrimaryHeaderEntity> statisList = sdsPrimaryHeaderService.list(params);
        //二级指标
        List<SdsSecondaryHeaderEntity> list = sdsSecondaryHeaderService.list(params);

        //按月份统计数据
        String month = (String) params.get("month");
        String year = (String) params.get("year");
        if (!StringUtils.isEmpty(month)){
            String month2 = year + "-" + month;
            params.put("month", month2);
        }
        //根据出院日期查询患者
        List<AnalysisIndexDto> stemiIndexStaDtos = sdsQcincaseService.getAnalysisIndexDatas(params);
        List<Map<String,String>> res = new ArrayList<>();
        for (AnalysisIndexDto analysisIndexDto : stemiIndexStaDtos){
            String dishDate = analysisIndexDto.getDishDate();
            params.put("dishDate", dishDate);
            Map<String,String> resMap = new HashMap<>();
            resMap.put("dishDate", dishDate);
            resMap.put("num", analysisIndexDto.getNum());
            //res.add(resMap);
            //按月展示的人数
            List<Long> episodeIds = sdsQcincaseService.getIndexNums(params);
            Map<String, Object> cal = new HashMap<>();
            for (Long l : episodeIds){
                params.put("episodeId", l);
                SdsQcincaseEntity sdsQcincaseEntity = sdsQcincaseService.selectByEpisodeId(params);
                Long inCaseId = sdsQcincaseEntity.getId();
                //每个人一个病种的各项指标

                for (SdsSecondaryHeaderEntity sdsSecondaryHeaderEntity : list){
                    Map<String, Object> map = new HashMap<>();
                    if (StringUtils.isEmpty(cal.get(sdsSecondaryHeaderEntity.getField()))){
                        cal.put(sdsSecondaryHeaderEntity.getField(), 0);
                    }
                    map.put("secondaryHeaderId", sdsSecondaryHeaderEntity.getId());
                    List<SdsIndexFieldEntity> list1 = sdsIndexFieldMapper.list(map);
                    String s = "";
                    String valueByFormIdAndCaseId = "";
                    String typeCode = "";
                    for (SdsIndexFieldEntity sdsIndexFieldEntity : list1){
                        //获取填报项目值
                        /*if (!StringUtils.isEmpty(sdsIndexFieldEntity.getFormItemId())){
                            valueByFormIdAndCaseId = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(sdsIndexFieldEntity.getFormItemId(), inCaseId);
                            //项目类型
                            SdsQcformItemEntity sdsQcformItemEntity = sdsQcformItemMapper.selectById(sdsIndexFieldEntity.getFormItemId());
                            DictionaryEntity dictionaryEntity = dictionaryService.selectById(sdsQcformItemEntity.getBtType());
                            typeCode = dictionaryEntity.getCode();
                        }
                        //获取指标配置值域
                        if (!StringUtils.isEmpty(sdsIndexFieldEntity.getFormDicId())){
                            SdsQcfitemDicEntity sdsQcfitemDicEntity = sdsQcfitemDicService.selectById(sdsIndexFieldEntity.getFormDicId());
                            s += sdsQcfitemDicEntity.getCode();
                        }*/
                    }
                    if (!StringUtils.isEmpty(valueByFormIdAndCaseId)){
                        if (Objects.equals("Num", typeCode)){
                            Object o = cal.get(sdsSecondaryHeaderEntity.getField());
                            double x = Double.parseDouble(o.toString());
                            double y = Double.parseDouble(valueByFormIdAndCaseId);
                            cal.put(sdsSecondaryHeaderEntity.getField(), x + y);
                        }else {
                            if (s.contains(valueByFormIdAndCaseId)){
                                Object o = cal.get(sdsSecondaryHeaderEntity.getField());
                                cal.put(sdsSecondaryHeaderEntity.getField(),Integer.parseInt(o.toString())+1);
                            }
                        }
                    }

                }

            }
            for (HashMap.Entry<String, Object> entry : cal.entrySet()){
                String key = entry.getKey();
                if (Objects.equals("field19", key) || Objects.equals("field20", key) || Objects.equals("field21", key) || Objects.equals("field22", key)){
                    Object o = cal.get(key);
                    resMap.put(key, o.toString());
                }else {
                    Object o = cal.get(key);
                    int i = Integer.parseInt(o.toString());
                    resMap.put(key, StrUtil.deciFormat(i, episodeIds.size()));
                }
            }
            res.add(resMap);
        }
        jsonObject.put("tableColumnAs", statisList);
        jsonObject.put("tableColumnBs", list);
        jsonObject.put("tableDates", res);
        return jsonObject;
    }

    @Override
    public JSONObject selectSdsIndColumns2(Map<String, Object> params) {
        JSONObject jsonObject = new JSONObject();
        //统计图数据
        JSONArray chartDatas = new JSONArray();
        JSONArray admDaysDatas = new JSONArray();
        JSONArray entityFeeDatas = new JSONArray();
        //一级指标
        params.put("headerType",1);
        params.put("indexType",1);
        List<SdsIndexHeaderEntity> parentList = sdsIndexHeaderService.list(params);
        for (SdsIndexHeaderEntity sdsIndexHeaderEntity : parentList){
            Map<String, Object> map = new HashMap<>();
            map.put("parentId", sdsIndexHeaderEntity.getId());
            List<SdsIndexHeaderEntity> list = sdsIndexHeaderService.list(map);
            sdsIndexHeaderEntity.setColspan(list.size());
        }
        //末级指标
        params.put("headerType",2);
        List<SdsIndexHeaderEntity> sonList = sdsIndexHeaderService.list(params);
        for (SdsIndexHeaderEntity sdsIndexHeaderEntity : sonList){
            sdsIndexHeaderEntity.setFieldNum(sdsIndexHeaderEntity.getField()+"Num");
        }
        //计算数据
        String month = (String) params.get("month");
        String year = (String) params.get("year");
        if (!StringUtils.isEmpty(month)){
            String month2 = year + "-" + month;
            params.put("month", month2);
        }
        //根据出院日期查询患者
        List<AnalysisIndexDto> stemiIndexStaDtos = sdsQcincaseService.getAnalysisIndexDatas(params);
        List<Map<String,Object>> res = new ArrayList<>();
        for (AnalysisIndexDto analysisIndexDto : stemiIndexStaDtos){
            String dishDate = analysisIndexDto.getDishDate();

            params.put("dishDate", dishDate);
            Map<String,Object> resMap = new HashMap<>();
            resMap.put("dishDate", dishDate);
            resMap.put("num", analysisIndexDto.getNum());
            //按月展示的人数
            List<Long> episodeIds = sdsQcincaseService.getIndexNums(params);
            Map<String, Object> cal = new HashMap<>();
            //存储图形数据
            Map<String, Object> cal2 = new TreeMap<>();
            for (Long l : episodeIds){
                params.put("episodeId", l);
                SdsQcincaseEntity sdsQcincaseEntity = sdsQcincaseService.selectByEpisodeId(params);
                Long inCaseId = sdsQcincaseEntity.getId();
                //每个人一个病种的各项指标
                for (SdsIndexHeaderEntity sdsIndexHeaderEntity : sonList){
                    if (StringUtils.isEmpty(cal.get(sdsIndexHeaderEntity.getField()))){
                        cal.put(sdsIndexHeaderEntity.getField(), 0);
                        if (Objects.equals(1, sdsIndexHeaderEntity.getAnalysisStatus())){
                            cal2.put(sdsIndexHeaderEntity.getField(), 0);
                        }
                    }
                    Map<String, Object> map = new HashMap<>();
                    map.put("indexHeaderId", sdsIndexHeaderEntity.getId());
                    List<SdsIndexFieldEntity> sdsIndexFieldEntityList = sdsIndexFieldService.list(map);
                    // 创建表达式引擎对象
                    JexlEngine engine = new JexlEngine();
                    // 创建表达式语句
                    String expressionStr = sdsIndexHeaderEntity.getExpress();
                    // 创建Context对象，为表达式中的未知数赋值
                    JexlContext context = new MapContext();
                    for (SdsIndexFieldEntity sdsIndexFieldEntity : sdsIndexFieldEntityList){
                        String valueByFormIdAndCaseId = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(sdsIndexFieldEntity.getItemId(), inCaseId);
                        if (!StringUtils.isEmpty(valueByFormIdAndCaseId) && !StringUtils.isEmpty(sdsIndexHeaderEntity.getExpress())){
                            if (sdsIndexHeaderEntity.getField().contains("Time")){
                                //对日期进行特殊处理
                                Date date = DateUtil.parseTime(valueByFormIdAndCaseId);
                                context.set(sdsIndexFieldEntity.getItemCode(),date);
                            }else {
                                context.set(sdsIndexFieldEntity.getItemCode(),valueByFormIdAndCaseId);
                            }
                        }else {
                            //
                            if (sdsIndexHeaderEntity.getField().contains("Time")){
                                context.set(sdsIndexFieldEntity.getItemCode(),new Date());
                            }else if (sdsIndexHeaderEntity.getField().contains("Fee")){
                                context.set(sdsIndexFieldEntity.getItemCode(),0);
                            }else {
                                context.set(sdsIndexFieldEntity.getItemCode(),"");
                            }
                        }
                    }
                    Object evaluate = null;
                    if (!StringUtils.isEmpty(expressionStr)){
                        // 使用表达式引擎创建表达式对象
                        Expression expression = engine.createExpression(expressionStr);
                        // 使用表达式对象计算
                        evaluate = expression.evaluate(context);
                    }
                    if (sdsIndexHeaderEntity.getField().contains("Fee")){
                        //费用
                        Object o = cal.get(sdsIndexHeaderEntity.getField());
                        double x = Double.parseDouble(o.toString());
                        double y =  Double.parseDouble(evaluate.toString());
                        cal.put(sdsIndexHeaderEntity.getField(), x + y);
                    }else if (sdsIndexHeaderEntity.getField().contains("Day")){
                        //天数
                        Object o = cal.get(sdsIndexHeaderEntity.getField());
                        int x = Integer.parseInt(o.toString());
                        int y =  Integer.parseInt(evaluate.toString());
                        cal.put(sdsIndexHeaderEntity.getField(), x + y);
                    }else {
                        //占比
                        if (Objects.equals(true, evaluate)){
                            Object o = cal.get(sdsIndexHeaderEntity.getField());
                            cal.put(sdsIndexHeaderEntity.getField(),Integer.parseInt(o.toString())+1);
                            if (Objects.equals(1, sdsIndexHeaderEntity.getAnalysisStatus())){
                                cal2.put(sdsIndexHeaderEntity.getField(),Integer.parseInt(o.toString())+1);
                                //cal2.put(sdsIndexHeaderEntity.getField(),cal.get(sdsIndexHeaderEntity.getField()));
                            }
                        }
                    }
                }
            }
            for (HashMap.Entry<String, Object> entry : cal.entrySet()){
                String key = entry.getKey();
                if (key.contains("Fee")){
                    double o = (double) cal.get(key);
                    resMap.put(key, StrUtil.deciFormatDouble(o, episodeIds.size()));
                    resMap.put(key+"Num", o);
                }else if (key.contains("Day")){
                    int o = (int) cal.get(key);
                    resMap.put(key, StrUtil.deciFormatU(o, episodeIds.size()));
                    resMap.put(key+"Num", o);
                }
                else {
                    Object o = cal.get(key);
                    int i = Integer.parseInt(o.toString());
                    resMap.put(key, StrUtil.deciFormat(i, episodeIds.size()));
                    resMap.put(key+"Num", i);
                }
            }
            res.add(resMap);
            //配置启用统计图
            for (HashMap.Entry<String, Object> entry : cal2.entrySet()){
                String key = entry.getKey();
                Object o = cal.get(key);
                int i = 0;
                double fee = 0;
                if (key.contains("Fee")){
                    fee = Double.parseDouble(o.toString());
                }else {
                    i = Integer.parseInt(o.toString());
                }
                /*JSONArray chartRate = new JSONArray();
                chartRate.add(splitJSONObj(dishDate, StrUtil.deciFormatM(i, episodeIds.size()), key));
                chartDatas.add(chartRate);*/
                Map<String,JSONArray> map = new HashMap<>();
                if (chartDatas.size() == 0){
                    JSONArray chartRate = new JSONArray();
                    if (key.contains("Fee")){
                        chartRate.add(splitJSONObj(dishDate, StrUtil.deciFormatDouble(fee, episodeIds.size()), key));
                    }else if (key.contains("Day")){
                        chartRate.add(splitJSONObj(dishDate, StrUtil.deciFormatU(i, episodeIds.size()), key));
                    } else{
                        chartRate.add(splitJSONObj(dishDate, StrUtil.deciFormatM(i, episodeIds.size()), key));
                    }
                    chartDatas.add(chartRate);
                }else {
                    a:for (int m = 0 ;m < chartDatas.size(); m++){
                        JSONArray jsonArray = chartDatas.getJSONArray(m);
                        for (int j = 0 ;j < jsonArray.size(); j++){
                            JSONObject jsonObject1 = jsonArray.getJSONObject(j);
                            Set<String> strings = jsonObject1.keySet();
                            for (String s : strings){
                                if (Objects.equals(s, "ddesc")){
                                    continue;
                                }else if (Objects.equals(s, key)){
                                    map.put(key, jsonArray);
                                    break a;
                                }else if (!Objects.equals(s, key)){
                                    map.put(key, null);
                                }
                            }

                        }
                    }
                }

                for (HashMap.Entry<String, JSONArray> entry1 : map.entrySet()){
                    String key1 = entry1.getKey();
                    JSONArray jsonArray = map.get(key1);
                    if (!StringUtils.isEmpty(jsonArray)){
                        if (key1.contains("Fee")){
                            jsonArray.add(splitJSONObj(dishDate, StrUtil.deciFormatDouble(fee, episodeIds.size()), key));
                        }else if (key1.contains("Day")){
                            jsonArray.add(splitJSONObj(dishDate, StrUtil.deciFormatU(i, episodeIds.size()), key));
                        } else {
                            jsonArray.add(splitJSONObj(dishDate, StrUtil.deciFormatM(i, episodeIds.size()), key));
                        }
                    }else {
                        JSONArray chartRate = new JSONArray();
                        //chartRate.add(splitJSONObj(dishDate, StrUtil.deciFormatM(i, episodeIds.size()), key));
                        if (key1.contains("Fee")){
                            chartRate.add(splitJSONObj(dishDate, StrUtil.deciFormatDouble(fee, episodeIds.size()), key));
                        }else if (key1.contains("Day")){
                            chartRate.add(splitJSONObj(dishDate, StrUtil.deciFormatU(i, episodeIds.size()), key));
                        }else {
                            chartRate.add(splitJSONObj(dishDate, StrUtil.deciFormatM(i, episodeIds.size()), key));
                        }
                        chartDatas.add(chartRate);
                    }

                }

            }

        }

        JSONArray chartDatas2 = new JSONArray();
        //BeanUtils.copyProperties(chartDatas, chartDatas2);
        chartDatas2.addAll(chartDatas);
        for (int m = 0 ;m < chartDatas.size(); m++){
            JSONArray jsonArray = chartDatas.getJSONArray(m);
            a:for (int j = 0 ;j < jsonArray.size(); j++){
                JSONObject jsonObject1 = jsonArray.getJSONObject(j);
                Set<String> strings = jsonObject1.keySet();
                for (String s : strings){
                    if (s.contains("Fee")){
                        chartDatas2.remove(jsonArray);
                        entityFeeDatas.add(jsonArray);
                        break a;
                    }else if (s.contains("Day")){
                        chartDatas2.remove(jsonArray);
                        admDaysDatas.add(jsonArray);
                        break a;
                    }
                }

            }
        }

        /*a:for (int m = 0 ;m < chartDatas.size(); m++){
            JSONArray jsonArray = chartDatas.getJSONArray(m);
            for (int j = 0 ;j < jsonArray.size(); j++){
                JSONObject jsonObject1 = jsonArray.getJSONObject(j);
                Set<String> strings = jsonObject1.keySet();
                for (String s : strings){
                    if (s.contains("Day")){
                        chartDatas.remove(jsonArray);
                        admDaysDatas.add(jsonArray);
                        break a;
                    }
                }

            }
        }*/


        jsonObject.put("tableColumnAs", parentList);
        jsonObject.put("tableColumnBs", sonList);
        jsonObject.put("tableDates", res);
        jsonObject.put("chartDates", chartDatas2);
        jsonObject.put("entityFeeDatas", entityFeeDatas);
        jsonObject.put("admDaysDatas", admDaysDatas);
        return jsonObject;
    }

    @Override
    public JSONObject selectSdsIndDetailColumns(Map<String, Object> params) {
        JSONObject jsonObject = new JSONObject();
        JSONArray feeArray = new JSONArray();
        //一级指标
        params.put("headerType",1);
        params.put("indexType",2);
        List<SdsIndexHeaderEntity> parentList = sdsIndexHeaderService.list(params);
        for (SdsIndexHeaderEntity sdsIndexHeaderEntity : parentList){
            Map<String, Object> map = new HashMap<>();
            map.put("parentId", sdsIndexHeaderEntity.getId());
            List<SdsIndexHeaderEntity> list = sdsIndexHeaderService.list(map);
            sdsIndexHeaderEntity.setColspan(list.size());
        }
        //末级指标
        params.put("headerType",2);
        List<SdsIndexHeaderEntity> sonList = sdsIndexHeaderService.list(params);

        List<Map<String,Object>> res = new ArrayList<>();
        List<Long> episodeIds = sdsQcincaseService.getIndexNums(params);
        Map<String,Double> calMap = new HashMap<>();
        for (Long l : episodeIds){
            params.put("episodeId", l);
            SdsQcincaseEntity sdsQcincaseEntity = sdsQcincaseService.selectByEpisodeId(params);
            Long inCaseId = sdsQcincaseEntity.getId();
            String name = cdcEpisodeService.getNameByEpisodeId(l);
            Map<String,Object> resMap = new HashMap<>();
            resMap.put("name", name);
            //每个人一个病种的各项指标
            for (SdsIndexHeaderEntity sdsIndexHeaderEntity : sonList){
                Map<String, Object> map = new HashMap<>();
                map.put("indexHeaderId", sdsIndexHeaderEntity.getId());
                List<SdsIndexFieldEntity> sdsIndexFieldEntityList = sdsIndexFieldService.list(map);
                // 创建表达式引擎对象
                JexlEngine engine = new JexlEngine();
                // 创建表达式语句
                String expressionStr = sdsIndexHeaderEntity.getExpress();
                // 创建Context对象，为表达式中的未知数赋值
                JexlContext context = new MapContext();
                //String dicDesc = "";
                Map<String, Long> itemRes = new HashMap<>();
                for (SdsIndexFieldEntity sdsIndexFieldEntity : sdsIndexFieldEntityList){
                    String valueByFormIdAndCaseId = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(sdsIndexFieldEntity.getItemId(), inCaseId);
                    if (!StringUtils.isEmpty(valueByFormIdAndCaseId) && !StringUtils.isEmpty(sdsIndexHeaderEntity.getExpress())){
                        if (sdsIndexHeaderEntity.getField().contains("Time")){
                            //对日期进行特殊处理
                            Date date = DateUtil.parseTime(valueByFormIdAndCaseId);
                            context.set(sdsIndexFieldEntity.getItemCode(),date);
                        }else if (sdsIndexHeaderEntity.getField().contains("Value")){
                            //取值域字典的描述，不做计算
                            itemRes.put(sdsIndexFieldEntity.getItemCode(), sdsIndexFieldEntity.getItemId());
                            //dicDesc = sdsQcfitemDicService.getDicDesc(sdsIndexFieldEntity.getItemId(), valueByFormIdAndCaseId);
                        } else {
                            context.set(sdsIndexFieldEntity.getItemCode(),valueByFormIdAndCaseId);
                        }
                    }else {
                        //
                        if (sdsIndexHeaderEntity.getField().contains("Fee")){
                            context.set(sdsIndexFieldEntity.getItemCode(),0);
                        }else {
                            context.set(sdsIndexFieldEntity.getItemCode(),"");
                        }
                    }
                }
                Object evaluate = null;
                String dicDesc = "";
                if (!StringUtils.isEmpty(expressionStr)){
                    // 使用表达式引擎创建表达式对象
                    Expression expression = engine.createExpression(expressionStr);
                    // 使用表达式对象计算
                    evaluate = expression.evaluate(context);
                }
                if (sdsIndexHeaderEntity.getField().contains("Value")){
                    //值域字典的描述
                    if (!StringUtils.isEmpty(evaluate)){
                        String resCode = evaluate.toString();
                        String[] split = resCode.split(",");
                        for (String s : split){
                            Long itemId = itemRes.get(s);
                            dicDesc = sdsQcfitemDicService.getDicDesc(itemId, s) + ";";
                        }
                    }
                    resMap.put(sdsIndexHeaderEntity.getField(), dicDesc);
                } else {
                    resMap.put(sdsIndexHeaderEntity.getField(), evaluate);
                }
            }
            res.add(resMap);

            //求统计图费用
            for (SdsIndexHeaderEntity sdsIndexHeaderEntity : sonList){
                if (sdsIndexHeaderEntity.getField().contains("Fee")){
                    if (sdsIndexHeaderEntity.getTitle().contains("住院总费用")){
                        continue;
                    }
                    Double o = calMap.get(sdsIndexHeaderEntity.getField());
                    if (StringUtils.isEmpty(o)){
                        o = 0.0;
                    }
                    double n = Double.parseDouble(resMap.get(sdsIndexHeaderEntity.getField()).toString());
                    if (StringUtils.isEmpty(n)){
                        n = 0.0;
                    }
                    calMap.put(sdsIndexHeaderEntity.getField(), o + n);
                }
            }


        }

        //费用占比饼图
        for (HashMap.Entry<String, Double> entry : calMap.entrySet()){
            String key = entry.getKey();
            String title = sdsIndexHeaderService.getTitleByField(Long.parseLong(params.get("qcEntity").toString()), 2, key);
            String fee = calMap.get(key).toString();
            feeArray.add(getJSONObj(2,"name",title,"value",fee,"",""));
        }


        jsonObject.put("tableColumnAs", parentList);
        jsonObject.put("tableColumnBs", sonList);
        jsonObject.put("tableDates", res);
        jsonObject.put("feeArray", feeArray);
        return jsonObject;
    }

    @Override
    public JSONObject selectSdsChartDatas(Map<String, Object> params) {
        JSONObject chartDataObj = new JSONObject();
        //上报例数
        JSONArray chartDatas = new JSONArray();
        JSONArray confirmRate = new JSONArray();
        List<AnalysisSdsMstaUpTimeDto> analysisSdsMstaUpDtos = new ArrayList<>();
        List<AnalysisSdsMstaUpTimeDto> analysisSdsMstaDayDtos = new ArrayList<>();
        List<AnalysisSdsMstaUpTimeDto> analysisSdsMstaFeeDtos = new ArrayList<>();
        String month = (String) params.get("month");
        if (StringUtils.isEmpty(month)){
            //按月份展示
            analysisSdsMstaUpDtos = analysisSdsMstaMapper.selectUpDataByMonth(params);
        }else {
            //按天展示
            analysisSdsMstaUpDtos = analysisSdsMstaMapper.selectUpDataByDay(params);
        }
        /*confirmRate.add(splitJSONObj("2021-01", 0, "upNum"));
        confirmRate.add(splitJSONObj("2021-03", 10, "upNum"));
        confirmRate.add(splitJSONObj("2021-05", 25, "upNum"));*/
        for (AnalysisSdsMstaUpTimeDto analysisSdsMstaUpTimeDto : analysisSdsMstaUpDtos){
            confirmRate.add(splitJSONObj(analysisSdsMstaUpTimeDto.getTime(), analysisSdsMstaUpTimeDto.getTotal(), "upNum"));
        }
        chartDatas.add(confirmRate);
        chartDataObj.put("chartDatas", chartDatas);
        //住院日
        JSONArray chartDatas2 = new JSONArray();
        JSONArray confirmRate2 = new JSONArray();
        confirmRate2.add(splitJSONObj("2021-01", 0, "confirmRate2"));
        confirmRate2.add(splitJSONObj("2021-03", 60, "confirmRate2"));
        confirmRate2.add(splitJSONObj("2021-05", 100, "confirmRate2"));
        chartDatas2.add(confirmRate2);
        chartDataObj.put("chartDatas2", chartDatas2);
        //病种费用
        JSONArray chartDatas3 = new JSONArray();
        JSONArray confirmRate3 = new JSONArray();
        confirmRate3.add(splitJSONObj("2021-01", 0, "confirmRate3"));
        confirmRate3.add(splitJSONObj("2021-03", 80, "confirmRate3"));
        confirmRate3.add(splitJSONObj("2021-05", 200, "confirmRate3"));
        chartDatas3.add(confirmRate3);
        chartDataObj.put("chartDatas3", chartDatas3);

        return chartDataObj;
    }

    @Override
    public JSONObject selectSysSumChart(Map<String, Object> params) {
        JSONObject caseObj = new JSONObject();
        if (StringUtils.isEmpty(params.get("aLocID"))){
            // 查询登录用户关联科室
            List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
            params.put("userLinkDepts", deptIds);
        }
        List<AnalysisSdsPortalVo> res = sdsQcincaseService.selectCostsAndDaysByDate(params);
        DecimalFormat df = new DecimalFormat("0.00");
        for (AnalysisSdsPortalVo analysisSdsPortalVo : res){
            String averageCosts = df.format((analysisSdsPortalVo.getCosts())/(analysisSdsPortalVo.getCount()));
            String averageDays = df.format((float)(analysisSdsPortalVo.getDays())/(analysisSdsPortalVo.getCount()));
            analysisSdsPortalVo.setAverageCosts(averageCosts);
            analysisSdsPortalVo.setAverageDays(averageDays);
        }
        caseObj.put("res", res);
        return caseObj;
    }

    @Override
    public JSONObject selectIndexTableData(Map<String, Object> params) {
        Date startDate = new Date();
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isEmpty(params.get("aLocID"))){
            List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
            params.put("userLinkDepts", deptIds);
        }
        //一级指标
        params.put("headerType",1);
        params.put("indexType",1);
        List<SdsIndexHeaderEntity> parentList = sdsIndexHeaderService.list(params);
        for (SdsIndexHeaderEntity sdsIndexHeaderEntity : parentList){
            Map<String, Object> map = new HashMap<>();
            map.put("parentId", sdsIndexHeaderEntity.getId());
            List<SdsIndexHeaderEntity> list = sdsIndexHeaderService.list(map);
            sdsIndexHeaderEntity.setColspan(list.size());
        }
        //末级指标
        params.put("headerType",2);
        List<SdsIndexHeaderEntity> sonList = sdsIndexHeaderService.list(params);
        for (SdsIndexHeaderEntity sdsIndexHeaderEntity : sonList){
            sdsIndexHeaderEntity.setFieldNum(sdsIndexHeaderEntity.getField()+"Num");
        }
        //计算数据
        //根据出院日期查询上报患者
        //List<AnalysisSdsPortalVo> analysisSdsPortalVoList = sdsQcincaseService.getIndexDataByDate(params);
        List<AnalysisSdsPortalVo> sdsPortalVoList = sdsQcincaseService.getIndexListDataByDate(params);
        List<AnalysisSdsPortalVo> analysisSdsPortalVoList = this.handleIndexListData(sdsPortalVoList);
        List<Map<String,Object>> res = new ArrayList<>();
        //单线程版
        /*for(AnalysisSdsPortalVo analysisSdsPortalVo : analysisSdsPortalVoList){
            long start1 = System.currentTimeMillis();
            Map<String,Object> resMap = new HashMap<>();
            Map<String, Object> cal = new HashMap<>();
            Map<String, Object> calDen = new HashMap<>();
            resMap.put("dishDate", analysisSdsPortalVo.getDate());
            resMap.put("ids", analysisSdsPortalVo.getIds());
            resMap.put("num", analysisSdsPortalVo.getCount());
            resMap.put("averageDays", StrUtil.deciFormatU(analysisSdsPortalVo.getDays(),analysisSdsPortalVo.getCount()));
            String inCaseIds = analysisSdsPortalVo.getIds();
            String[] split = inCaseIds.split(",");
            for (String s : split){
                long start2 = System.currentTimeMillis();
                Long inCaseId = Long.parseLong(s);
                //每个人一个病种的各项指标
                a:for (SdsIndexHeaderEntity sdsIndexHeaderEntity : sonList){
                    //指标字段未配置
                    if (StringUtils.isEmpty(cal.get(sdsIndexHeaderEntity.getField()))){
                        cal.put(sdsIndexHeaderEntity.getField(), 0);
                    }
                    List<SdsIndexFieldEntity> sdsIndexFieldEntityList = sdsIndexFieldService.selectList(new EntityWrapper<SdsIndexFieldEntity>()
                    .eq("index_header_id", sdsIndexHeaderEntity.getId()));
                    // 创建表达式引擎对象
                    JexlEngine engine = new JexlEngine();
                    // 创建表达式语句
                    String expressionStr = sdsIndexHeaderEntity.getExpress();
                    String expressionDenStr = sdsIndexHeaderEntity.getExpressDen();
                    // 创建Context对象，为表达式中的未知数赋值
                    JexlContext context = new MapContext();
                    JexlContext contextDen = new MapContext();
                    b:for (SdsIndexFieldEntity sdsIndexFieldEntity : sdsIndexFieldEntityList){
                        if (sdsIndexFieldEntity.getItemType()!=1) {
                            continue ;
                        }
                        // 根据前置条件，计算项目显隐属性
                        Integer editPower = this.calcTriggerExp(inCaseId, FormItemCache.me().getFormItemById(sdsIndexFieldEntity.getItemId()), new HashMap<>());
                        if (editPower == 0) {
                            continue a;
                        }
                        String valueByFormIdAndCaseId = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(sdsIndexFieldEntity.getItemId(), inCaseId);
                        if (!StringUtils.isEmpty(valueByFormIdAndCaseId) && !StringUtils.isEmpty(sdsIndexHeaderEntity.getExpress())){
                            //对日期进行特殊处理
                            if (sdsIndexHeaderEntity.getField().contains("Time")){
                                if (DateUtil.isValidDate(valueByFormIdAndCaseId)) {
                                    valueByFormIdAndCaseId += " 00:00:00";
                                }
                                Date date = null;
                                if (DateUtil.isValidDateTime(valueByFormIdAndCaseId)) {
                                    date = DateUtil.parseTime(valueByFormIdAndCaseId);
                                }
                                context.set(sdsIndexFieldEntity.getItemCode(), date!=null?date:valueByFormIdAndCaseId);
                            }else {
                                context.set(sdsIndexFieldEntity.getItemCode(),valueByFormIdAndCaseId);
                            }
                        }else {
                            //对表达式为空和项目值为空进行处理
                            if (sdsIndexHeaderEntity.getField().contains("Time")){
                                context.set(sdsIndexFieldEntity.getItemCode(),new Date());
                            }else if (sdsIndexHeaderEntity.getField().contains("Fee")){
                                context.set(sdsIndexFieldEntity.getItemCode(),0);
                            }else {
                                context.set(sdsIndexFieldEntity.getItemCode(),"");
                            }
                            continue a;
                        }
                    }

                    c:
                    for (SdsIndexFieldEntity sdsIndexFieldEntity : sdsIndexFieldEntityList) {
                        if (sdsIndexFieldEntity.getItemType()!=2) {
                            continue ;
                        }
                        // 根据前置条件，计算项目显隐属性
                        Integer editPower = this.calcTriggerExp(inCaseId, FormItemCache.me().getFormItemById(sdsIndexFieldEntity.getItemId()), new HashMap<>());
                        if (editPower == 0) {
                            continue a;
                        }

                        String valueByFormIdAndCaseId = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(sdsIndexFieldEntity.getItemId(), inCaseId);
                        if (!StringUtils.isEmpty(valueByFormIdAndCaseId) && !StringUtils.isEmpty(sdsIndexHeaderEntity.getExpressDen())){
                            //对日期进行特殊处理
                            if (sdsIndexHeaderEntity.getField().contains("Time")){
                                if (DateUtil.isValidDate(valueByFormIdAndCaseId)) {
                                    valueByFormIdAndCaseId += " 00:00:00";
                                }
                                Date date = null;
                                if (DateUtil.isValidDateTime(valueByFormIdAndCaseId)) {
                                    date = DateUtil.parseTime(valueByFormIdAndCaseId);
                                }
                                contextDen.set(sdsIndexFieldEntity.getItemCode(),  date!=null?date:valueByFormIdAndCaseId);
                            }else {
                                contextDen.set(sdsIndexFieldEntity.getItemCode(), valueByFormIdAndCaseId);
                            }
                        }else {
                            //对表达式为空和项目值为空进行处理
                            if (sdsIndexHeaderEntity.getField().contains("Time")){
                                contextDen.set(sdsIndexFieldEntity.getItemCode(),new Date());
                            }else if (sdsIndexHeaderEntity.getField().contains("Fee")){
                                contextDen.set(sdsIndexFieldEntity.getItemCode(),0);
                            }else {
                                contextDen.set(sdsIndexFieldEntity.getItemCode(),"");
                            }
                        }
                    }

                    Object evaluate = null;
                    if (!StringUtils.isEmpty(expressionStr)){
                        // 使用表达式引擎创建表达式对象
                        Expression expression = engine.createExpression(expressionStr);
                        // 使用表达式对象计算
                        evaluate = expression.evaluate(context);
                    }
                    Object o = cal.get(sdsIndexHeaderEntity.getField());
                    //费用
                    if (sdsIndexHeaderEntity.getField().contains("Fee")){
                        double x = 0;
                        if (!StringUtils.isEmpty(o)){
                             x = Double.parseDouble(o.toString());
                        }
                        double y =  Double.parseDouble(evaluate.toString());
                        cal.put(sdsIndexHeaderEntity.getField(), x + y);
                    }else if (sdsIndexHeaderEntity.getField().contains("Day")){
                        //天数
                        int x = 0;
                        if (!StringUtils.isEmpty(o)){
                            x = Integer.parseInt(o.toString());
                        }
                        int y =  Integer.parseInt(evaluate.toString());
                        cal.put(sdsIndexHeaderEntity.getField(), x + y);
                    }else {
                        //占比
                        int x = 0;
                        if (!StringUtils.isEmpty(o)){
                            x = Integer.parseInt(o.toString());
                        }
                        if (Objects.equals(true, evaluate)){
                            cal.put(sdsIndexHeaderEntity.getField(),x+1);
                        } else {
                            cal.put(sdsIndexHeaderEntity.getField(),x);
                        }
                    }

                    if (!StringUtils.isEmpty(expressionDenStr)){
                        // 使用表达式引擎创建表达式对象
                        Expression expression = engine.createExpression(expressionDenStr);
                        // 使用表达式对象计算
                        Object evaluateDen = expression.evaluate(contextDen);
                        Object obj = calDen.get(sdsIndexHeaderEntity.getField());
                        //费用
                        if (sdsIndexHeaderEntity.getField().contains("Fee")){
                            double x = 0;
                            if (!StringUtils.isEmpty(obj)){
                                x = Double.parseDouble(obj.toString());
                            }
                            double y =  Double.parseDouble(evaluateDen.toString());
                            calDen.put(sdsIndexHeaderEntity.getField(), x + y);
                        }else if (sdsIndexHeaderEntity.getField().contains("Day")){
                            //天数
                            int x = 0;
                            if (!StringUtils.isEmpty(obj)){
                                x = Integer.parseInt(obj.toString());
                            }
                            int y =  Integer.parseInt(evaluateDen.toString());
                            calDen.put(sdsIndexHeaderEntity.getField(), x + y);
                        }else {
                            //占比
                            int x = 0;
                            if (!StringUtils.isEmpty(obj)){
                                x = Integer.parseInt(obj.toString());
                            }
                            if (Objects.equals(true, evaluateDen)){
                                calDen.put(sdsIndexHeaderEntity.getField(),x+1);
                            } else {
                                calDen.put(sdsIndexHeaderEntity.getField(), x);
                            }
                        }
                    }
                }
                System.out.println(cn.hutool.core.util.StrUtil.format("inCaseId = {}, 用时：{}ms",inCaseId,System.currentTimeMillis() - start2));
            }
            //处理数据
            for (HashMap.Entry<String, Object> entry : cal.entrySet()){
                String key = entry.getKey();
                Object o = cal.get(key);
                Object objDen = calDen.get(key);
                if (key.contains("Fee")){
                    double x = 0;
                    double y = 0;
                    if (!StringUtils.isEmpty(o)){
                        x = Double.parseDouble(o.toString());
                    }
                    if (!StringUtils.isEmpty(objDen)){
                        y = Double.parseDouble(objDen.toString());
                        resMap.put(key+"Den", y);
                    } else {
                        y = analysisSdsPortalVo.getCount();
                    }

                    Object val = null;
                    if (y == 0) {
                        val = 0;
                    } else {
                        val = StrUtil.deciFormatDouble(x, (int) y);
                    }
                    resMap.put(key, val);
                    resMap.put(key+"Num", o);
                }else if (key.contains("Day")){
                    int x = 0;
                    int y = 0;
                    if (!StringUtils.isEmpty(o)){
                        x = Integer.parseInt(o.toString());
                    }
                    if (!StringUtils.isEmpty(objDen)){
                        y = Integer.parseInt(objDen.toString());
                        resMap.put(key+"Den", y);
                    } else {
                        y = analysisSdsPortalVo.getCount();
                    }
                    Object val = null;
                    if (y == 0) {
                        val = 0;
                    } else {
                        val = StrUtil.deciFormatU(x, y);
                    }
                    resMap.put(key, val);
                    resMap.put(key+"Num", o);
                }
                else {
                    int x = Integer.parseInt(o.toString());
                    int y = 0;
                    if (!StringUtils.isEmpty(o)){
                        x = Integer.parseInt(o.toString());
                    }
                    if (!StringUtils.isEmpty(objDen)){
                        y = Integer.parseInt(objDen.toString());
                        resMap.put(key+"Den", y);
                    } else {
                        y = analysisSdsPortalVo.getCount();
                    }
                    Object val = StrUtil.deciFormat(x, y);
                    resMap.put(key, val);
                    resMap.put(key+"Num", x);
                }
            }
            System.out.println(cn.hutool.core.util.StrUtil.format("dishDate = {}, 用时：{}ms",analysisSdsPortalVo.getDate(),System.currentTimeMillis() - start1));
            res.add(resMap);
        }*/
        //多线程版
        ExecutorService analysisDataPools = Executors.newFixedThreadPool(10);//分析数据线程池
        CountDownLatch countDownLatch = new CountDownLatch(analysisSdsPortalVoList.size());//确定会启动10个线程任务
        for (AnalysisSdsPortalVo analysisSdsPortalVo : analysisSdsPortalVoList){
            analysisDataPools.execute(new IndexCalculationByMonth(analysisSdsPortalVo, sonList, res, countDownLatch));
        }
        try {
            countDownLatch.await();//多线程运行结束前自旋等待
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        jsonObject.put("tableColumnAs", parentList);
        jsonObject.put("tableColumnBs", sonList);
        jsonObject.put("tableDates", res.stream().sorted(Comparator.comparing(map -> map.get("dishDate").toString())).collect(Collectors.toList()));
        return jsonObject;
    }

    @Override
    public JSONObject selectIndexDetailTableData(Map<String, Object> params) {
        JSONObject jsonObject = new JSONObject();
        //一级指标
        params.put("headerType",1);
        params.put("indexType",2);
        List<SdsIndexHeaderEntity> parentList = sdsIndexHeaderService.list(params);
        for (SdsIndexHeaderEntity sdsIndexHeaderEntity : parentList){
            Map<String, Object> map = new HashMap<>();
            map.put("parentId", sdsIndexHeaderEntity.getId());
            List<SdsIndexHeaderEntity> list = sdsIndexHeaderService.list(map);
            sdsIndexHeaderEntity.setColspan(list.size());
        }
        //末级指标
        params.put("headerType",2);
        List<SdsIndexHeaderEntity> sonList = sdsIndexHeaderService.list(params);
        Object ids = params.get("ids");
        List<Long> incaseIds;
        if (StringUtils.isEmpty(ids)){
            //自定义查询
            incaseIds = this.selectByCondition(params);
        }else {
            String s1 = ids.toString();
            incaseIds = Arrays.asList(s1.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        }
        //每个人一个病种的各项指标
        List<Map<String,Object>> res = new ArrayList<>();
        for (Long incaseId : incaseIds){
            Map<String,Object> resMap = new HashMap<>();
            CdcEpisodeEntity episodeEntity = sdsQcincaseService.selectEpisodeByIncaseId(incaseId);
            resMap.put("name", episodeEntity.getName());
            resMap.put("admDays", episodeEntity.getAdmDays());
            for (SdsIndexHeaderEntity sdsIndexHeaderEntity : sonList){
                Map<String, Object> map = new HashMap<>();
                map.put("indexHeaderId", sdsIndexHeaderEntity.getId());
                List<SdsIndexFieldEntity> sdsIndexFieldEntityList = sdsIndexFieldService.list(map);
                // 创建表达式引擎对象
                JexlEngine engine = new JexlEngine();
                // 创建表达式语句
                String expressionStr = sdsIndexHeaderEntity.getExpress();
                // 创建Context对象，为表达式中的未知数赋值
                JexlContext context = new MapContext();
                //处理包含值域字典
                Map<String, Long> itemRes = new HashMap<>();
                for (SdsIndexFieldEntity sdsIndexFieldEntity : sdsIndexFieldEntityList){
                    String valueByFormIdAndCaseId = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(sdsIndexFieldEntity.getItemId(), incaseId);
                    if (!StringUtils.isEmpty(valueByFormIdAndCaseId) && !StringUtils.isEmpty(sdsIndexHeaderEntity.getExpress())){
                        if (sdsIndexHeaderEntity.getField().contains("Time")){
                            Date date = DateUtil.parseTime(valueByFormIdAndCaseId);
                            context.set(sdsIndexFieldEntity.getItemCode(),date);
                        }else if (sdsIndexHeaderEntity.getField().contains("Value")){
                            itemRes.put(valueByFormIdAndCaseId, sdsIndexFieldEntity.getItemId());
                            context.set(sdsIndexFieldEntity.getItemCode(),valueByFormIdAndCaseId);
                        }else {
                            context.set(sdsIndexFieldEntity.getItemCode(),valueByFormIdAndCaseId);
                        }
                    }else {
                        if (sdsIndexHeaderEntity.getField().contains("Fee")){
                            context.set(sdsIndexFieldEntity.getItemCode(),0);
                        }else {
                            context.set(sdsIndexFieldEntity.getItemCode(),"");
                        }
                    }
                }
                Object evaluate = null;
                String dicDesc = "";
                if (!StringUtils.isEmpty(expressionStr)){
                    // 使用表达式引擎创建表达式对象
                    Expression expression = engine.createExpression(expressionStr);
                    // 使用表达式对象计算
                    evaluate = expression.evaluate(context);
                }
                if (sdsIndexHeaderEntity.getField().contains("Value")){
                    //值域字典的描述
                    if (!StringUtils.isEmpty(evaluate)){
                        String resCode = evaluate.toString();
                        if (resCode.contains(",")){
                            String[] split = resCode.split(",");
                            for (String s : split){
                                Long itemId = itemRes.get(s);
                                dicDesc = sdsQcfitemDicService.getDicDesc(itemId, s) + ";";
                            }
                        }else {
                            Long itemId = itemRes.get(resCode);
                            dicDesc = sdsQcfitemDicService.getDicDesc(itemId, resCode) + ";";
                        }

                    }
                    resMap.put(sdsIndexHeaderEntity.getField(), dicDesc);
                } else {
                    resMap.put(sdsIndexHeaderEntity.getField(), evaluate);
                }
            }
            res.add(resMap);
        }
        jsonObject.put("tableColumnAs", parentList);
        jsonObject.put("tableColumnBs", sonList);
        jsonObject.put("tableDates", res);
        return jsonObject;
    }

    private List<Long> selectByCondition(Map<String, Object> params) {
        List<Long> incaseIds;
        if (StringUtils.isEmpty(params.get("aLocID"))){
            // 查询登录用户关联科室
            List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
            params.put("userLinkDepts", deptIds);
        }
        String dateType = (String) params.get("aDateType");
        String status = "";
        List<String> statusList = new ArrayList<>();
        if (!StringUtils.isEmpty(params.get("aStatus"))){
            status = (String) params.get("aStatus");
            String[] split = status.split(",");
            statusList = Arrays.asList(split);
        }if (Objects.equals("UpQC", dateType)){
            if (status.contains("Up") || StringUtils.isEmpty(status)){
                incaseIds = sdsQcincaseOperlogService.selectIncaseIdsByCondition(params);
            }else {
                //上报日期状态不为上报状态
                incaseIds = null;
            }

        }else {
            incaseIds = sdsQcincaseService.selectIncaseIdsByCondition(params,statusList);
        }
        return incaseIds;
    }

    private List<AnalysisSdsPortalVo> handleIndexListData(List<AnalysisSdsPortalVo> sdsPortalVoList) {
        List<AnalysisSdsPortalVo> result = new ArrayList<>();
        Map<String, List<AnalysisSdsPortalVo>> collect = sdsPortalVoList.stream().collect(Collectors.groupingBy(AnalysisSdsPortalVo::getDate));
        for (String date : collect.keySet()){
            AnalysisSdsPortalVo analysisSdsPortalVo = new AnalysisSdsPortalVo();
            List<AnalysisSdsPortalVo> analysisSdsPortalVoList = collect.get(date);
            String ids = "";
            Integer sumDays = 0;
            for (AnalysisSdsPortalVo sdsPortalVo : analysisSdsPortalVoList){
                ids += sdsPortalVo.getId() + ",";
                sumDays += sdsPortalVo.getDays();
            }
            analysisSdsPortalVo.setDate(date);
            analysisSdsPortalVo.setCount(analysisSdsPortalVoList.size());
            analysisSdsPortalVo.setIds(ids);
            analysisSdsPortalVo.setDays(sumDays);
            result.add(analysisSdsPortalVo);
        }
        return result;
    }

    public JSONObject splitJSONObj(String ddesc,Object rate,String code){
        JSONObject obj = new JSONObject();
        obj.put("ddesc", ddesc);
        obj.put(code, rate);
        return obj;
    }

    /**
     * @Description: 组装JSONObject
     * @return: JSONObject
     * @author: wujiafeng
     * @date:2020年1月3日 上午11:32:54
     */
    public JSONObject getJSONObj(int keyNum,String key1,String value1,String key2,String value2,String key3,String value3){
        JSONObject jsonObj = new JSONObject();
        if(keyNum==2){
            jsonObj.put(key1, value1);
            jsonObj.put(key2, value2);
        }else if(keyNum==3){
            jsonObj.put(key1, value1);
            jsonObj.put(key2, value2);
            jsonObj.put(key3, value3);
        }
        return jsonObj;
    }

    public class IndexCalculation extends Thread{
        private String inCaseId;
        private Map<String, Object> calData;
        private List<SdsIndexHeaderEntity> sonList;
        private CountDownLatch countDownLatch;

        public IndexCalculation(String inCaseId, Map<String, Object> calData, List<SdsIndexHeaderEntity> sonList, CountDownLatch countDownLatch){
            this.inCaseId = inCaseId;
            this.calData = calData;
            this.sonList = sonList;
            this.countDownLatch = countDownLatch;
        }

        public void run() {
            try {
                long start = System.currentTimeMillis();
                //消费
                IndexCalculationMain(inCaseId, calData, sonList);

                System.out.println(cn.hutool.core.util.StrUtil.format("inCaseId = {}, 用时：{}ms",inCaseId,System.currentTimeMillis() - start));

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                countDownLatch.countDown();//计数器减1
            }
        }

    }

    public void IndexCalculationMain(String inCaseId, Map<String, Object> calData, List<SdsIndexHeaderEntity> sonList) throws Exception {
        handleIndexData(inCaseId, calData, sonList);
        //这个时候处理完一个病例 多线程加锁
        synchronized (cur) {
            cur++;
        }
    }

    private void handleIndexData(String inCaseId, Map<String, Object> cal, List<SdsIndexHeaderEntity> sonList) {
        //每个人一个病种的各项指标
        a:for (SdsIndexHeaderEntity sdsIndexHeaderEntity : sonList){
            //指标字段未配置
            if (StringUtils.isEmpty(cal.get(sdsIndexHeaderEntity.getField()))){
                cal.put(sdsIndexHeaderEntity.getField(), 0);
            }
            List<SdsIndexFieldEntity> sdsIndexFieldEntityList = sdsIndexFieldService.selectList(new EntityWrapper<SdsIndexFieldEntity>()
                    .eq("index_header_id", sdsIndexHeaderEntity.getId()));
            // 创建表达式引擎对象
            JexlEngine engine = new JexlEngine();
            // 创建表达式语句
            String expressionStr = sdsIndexHeaderEntity.getExpress();
            // 创建Context对象，为表达式中的未知数赋值
            JexlContext context = new MapContext();
            b:for (SdsIndexFieldEntity sdsIndexFieldEntity : sdsIndexFieldEntityList){
                String valueByFormIdAndCaseId = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(sdsIndexFieldEntity.getItemId(), Long.parseLong(inCaseId));
                if (!StringUtils.isEmpty(valueByFormIdAndCaseId) && !StringUtils.isEmpty(sdsIndexHeaderEntity.getExpress())){
                    //对日期进行特殊处理
                    if (sdsIndexHeaderEntity.getField().contains("Time")){
                        Date date = DateUtil.parseTime(valueByFormIdAndCaseId);
                        context.set(sdsIndexFieldEntity.getItemCode(),date);
                    }else {
                        context.set(sdsIndexFieldEntity.getItemCode(),valueByFormIdAndCaseId);
                    }
                }else {
                    //对表达式为空和项目值为空进行处理
                    continue a;
                }
            }
            Object evaluate = null;
            if (!StringUtils.isEmpty(expressionStr)){
                // 使用表达式引擎创建表达式对象
                Expression expression = engine.createExpression(expressionStr);
                // 使用表达式对象计算
                evaluate = expression.evaluate(context);
            }
            Object o = cal.get(sdsIndexHeaderEntity.getField());
            //费用
            if (sdsIndexHeaderEntity.getField().contains("Fee")){
                double x = 0;
                if (!StringUtils.isEmpty(o)){
                    x = Double.parseDouble(o.toString());
                }
                double y =  Double.parseDouble(evaluate.toString());
                cal.put(sdsIndexHeaderEntity.getField(), x + y);
            }else if (sdsIndexHeaderEntity.getField().contains("Day")){
                //天数
                int x = 0;
                if (!StringUtils.isEmpty(o)){
                    x = Integer.parseInt(o.toString());
                }
                int y =  Integer.parseInt(evaluate.toString());
                cal.put(sdsIndexHeaderEntity.getField(), x + y);
            }else {
                //占比
                int x = 0;
                if (!StringUtils.isEmpty(o)){
                    x = Integer.parseInt(o.toString());
                }
                if (Objects.equals(true, evaluate)){
                    cal.put(sdsIndexHeaderEntity.getField(),x+1);
                }
            }
        }
    }

    public class IndexCalculationByMonth extends Thread {
        private AnalysisSdsPortalVo analysisSdsPortalVo;
        private List<SdsIndexHeaderEntity> sonList;
        private List<Map<String,Object>> res;
        private CountDownLatch countDownLatch;

        public IndexCalculationByMonth(AnalysisSdsPortalVo analysisSdsPortalVo, List<SdsIndexHeaderEntity> sonList,
                                       List<Map<String,Object>> res, CountDownLatch countDownLatch){
            this.analysisSdsPortalVo = analysisSdsPortalVo;
            this.sonList = sonList;
            this.res = res;
            this.countDownLatch = countDownLatch;
        }

        public void run() {
            try {
                long start = System.currentTimeMillis();
                //消费
                IndexCalculationByMonthMain(analysisSdsPortalVo, sonList, res);

                System.out.println(cn.hutool.core.util.StrUtil.format("date = {}, 用时：{}ms",analysisSdsPortalVo.getDate(),System.currentTimeMillis() - start));

            } catch (Exception e) {
                countDownLatch.countDown();//计数器减1
                e.printStackTrace();
            } finally {
                countDownLatch.countDown();//计数器减1
            }
        }
    }

    public void IndexCalculationByMonthMain(AnalysisSdsPortalVo analysisSdsPortalVo, List<SdsIndexHeaderEntity> sonList, List<Map<String,Object>> res) throws Exception {
        handleIndexDataByMonth(analysisSdsPortalVo, sonList, res);
        //这个时候处理完一个病例 多线程加锁
        synchronized (cur) {
            cur++;
        }
    }

    private void handleIndexDataByMonth(AnalysisSdsPortalVo analysisSdsPortalVo, List<SdsIndexHeaderEntity> sonList, List<Map<String, Object>> res) throws Exception {
        Map<String,Object> resMap = new HashMap<>();
        Map<String, Object> cal = new HashMap<>();
        Map<String, Object> calDen = new HashMap<>();
        resMap.put("dishDate", analysisSdsPortalVo.getDate());
        resMap.put("ids", analysisSdsPortalVo.getIds());
        resMap.put("num", analysisSdsPortalVo.getCount());
        resMap.put("averageDays", StrUtil.deciFormatU(analysisSdsPortalVo.getDays(),analysisSdsPortalVo.getCount()));
        String inCaseIds = analysisSdsPortalVo.getIds();
        String[] split = inCaseIds.split(",");
        // 指标数量
        int indexNum = 0;
        for (String s : split){
            long start2 = System.currentTimeMillis();
            Long inCaseId = Long.parseLong(s);
            //每个人一个病种的各项指标
            a:for (SdsIndexHeaderEntity sdsIndexHeaderEntity : sonList){
                //指标字段未配置
                if (StringUtils.isEmpty(cal.get(sdsIndexHeaderEntity.getField()))){
                    cal.put(sdsIndexHeaderEntity.getField(), 0);
                }
                List<SdsIndexFieldEntity> sdsIndexFieldEntityList = sdsIndexFieldService.selectList(new EntityWrapper<SdsIndexFieldEntity>()
                        .eq("index_header_id", sdsIndexHeaderEntity.getId()));
                if (sdsIndexHeaderEntity.getField().contains("Index")) {
                    indexNum = sdsIndexFieldEntityList.size();
                }
                // 创建表达式引擎对象
                JexlEngine engine = new JexlEngine();
                // 创建表达式语句
                String expressionStr = sdsIndexHeaderEntity.getExpress();
                String expressionDenStr = sdsIndexHeaderEntity.getExpressDen();
                // 创建Context对象，为表达式中的未知数赋值
                JexlContext context = new MapContext();
                JexlContext contextDen = new MapContext();
                b:for (SdsIndexFieldEntity sdsIndexFieldEntity : sdsIndexFieldEntityList){
                    if (sdsIndexFieldEntity.getItemType()!=1) {
                        continue ;
                    }
                    if (sdsIndexHeaderEntity.getField().contains("Index")) {
                        SdsIndexHeaderEntity indexHeaderEntity = sdsIndexHeaderService.selectById(sdsIndexFieldEntity.getItemId());
                        context.set(sdsIndexFieldEntity.getItemCode(), cal.get(indexHeaderEntity.getField()));
                        continue;
                    }
                    if (sdsIndexHeaderEntity.getField().contains("Death") && sdsIndexFieldEntity.getItemId() == 1001) {
                        SdsQcincaseEntity sdsQcincaseEntity = sdsQcincaseService.selectById(inCaseId);
                        Long episodeId = sdsQcincaseEntity.getEpisodeId();
                        CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectById(episodeId);
                        context.set(sdsIndexFieldEntity.getItemCode(), cdcEpisodeEntity.getDeathDate());
                        continue;
                    }
                    if (sdsIndexHeaderEntity.getField().contains("Evaluate") && sdsIndexFieldEntity.getItemId() == 1002) {
                        SdsQcincaseEntity sdsQcincaseEntity = sdsQcincaseService.selectById(inCaseId);
                        Long episodeId = sdsQcincaseEntity.getEpisodeId();
                        CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectById(episodeId);
                        context.set(sdsIndexFieldEntity.getItemCode(), cdcEpisodeEntity.getIsEvaluate());
                        continue;
                    }
                    // 根据前置条件，计算项目显隐属性
                    Integer editPower = this.calcTriggerExp(inCaseId, FormItemCache.me().getFormItemById(sdsIndexFieldEntity.getItemId()), new HashMap<>());
                    if (editPower == 0) {
                        continue a;
                    }
                    String valueByFormIdAndCaseId = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(sdsIndexFieldEntity.getItemId(), inCaseId);
                    if (!StringUtils.isEmpty(valueByFormIdAndCaseId) && !StringUtils.isEmpty(sdsIndexHeaderEntity.getExpress())){
                        //对日期进行特殊处理
                        if (sdsIndexHeaderEntity.getField().contains("Time")){
                            if (DateUtil.isValidDate(valueByFormIdAndCaseId)) {
                                valueByFormIdAndCaseId += " 00:00:00";
                            }
                            Date date = null;
                            if (DateUtil.isValidDateTime(valueByFormIdAndCaseId)) {
                                date = DateUtil.parseTime(valueByFormIdAndCaseId);
                            }
                            context.set(sdsIndexFieldEntity.getItemCode(), date!=null?date:valueByFormIdAndCaseId);
                        }else {
                            context.set(sdsIndexFieldEntity.getItemCode(),valueByFormIdAndCaseId);
                        }
                    }else {
                        //对表达式为空和项目值为空进行处理
                            /*if (sdsIndexHeaderEntity.getField().contains("Time")){
                                context.set(sdsIndexFieldEntity.getItemCode(),new Date());
                            }else if (sdsIndexHeaderEntity.getField().contains("Fee")){
                                context.set(sdsIndexFieldEntity.getItemCode(),0);
                            }else {
                                context.set(sdsIndexFieldEntity.getItemCode(),"");
                            }*/
                        continue a;
                    }
                }

                c:
                for (SdsIndexFieldEntity sdsIndexFieldEntity : sdsIndexFieldEntityList) {
                    if (sdsIndexFieldEntity.getItemType()!=2) {
                        continue ;
                    }
                    // 根据前置条件，计算项目显隐属性
                    Integer editPower = this.calcTriggerExp(inCaseId, FormItemCache.me().getFormItemById(sdsIndexFieldEntity.getItemId()), new HashMap<>());
                    if (editPower == 0) {
                        continue a;
                    }

                    String valueByFormIdAndCaseId = sdsQcfitemExecresultService.getValueByFormIdAndCaseId(sdsIndexFieldEntity.getItemId(), inCaseId);
                    if (!StringUtils.isEmpty(valueByFormIdAndCaseId) && !StringUtils.isEmpty(sdsIndexHeaderEntity.getExpressDen())){
                        //对日期进行特殊处理
                        if (sdsIndexHeaderEntity.getField().contains("Time")){
                            if (DateUtil.isValidDate(valueByFormIdAndCaseId)) {
                                valueByFormIdAndCaseId += " 00:00:00";
                            }
                            Date date = null;
                            if (DateUtil.isValidDateTime(valueByFormIdAndCaseId)) {
                                date = DateUtil.parseTime(valueByFormIdAndCaseId);
                            }
                            contextDen.set(sdsIndexFieldEntity.getItemCode(),  date!=null?date:valueByFormIdAndCaseId);
                        }else {
                            contextDen.set(sdsIndexFieldEntity.getItemCode(), valueByFormIdAndCaseId);
                        }
                    }else {
                        //对表达式为空和项目值为空进行处理
                        if (sdsIndexHeaderEntity.getField().contains("Time")){
                            contextDen.set(sdsIndexFieldEntity.getItemCode(),new Date());
                        }else if (sdsIndexHeaderEntity.getField().contains("Fee")){
                            contextDen.set(sdsIndexFieldEntity.getItemCode(),0);
                        }else {
                            contextDen.set(sdsIndexFieldEntity.getItemCode(),"");
                        }
                    }
                }

                Object evaluate = null;
                if (!StringUtils.isEmpty(expressionStr)){
                    // 使用表达式引擎创建表达式对象
                    Expression expression = engine.createExpression(expressionStr);
                    // 使用表达式对象计算
                    evaluate = expression.evaluate(context);
                }
                Object o = cal.get(sdsIndexHeaderEntity.getField());
                //费用
                if (sdsIndexHeaderEntity.getField().contains("Fee")){
                    double x = 0;
                    if (!StringUtils.isEmpty(o)){
                        x = Double.parseDouble(o.toString());
                    }
                    double y =  Double.parseDouble(evaluate.toString());
                    cal.put(sdsIndexHeaderEntity.getField(), x + y);
                }else if (sdsIndexHeaderEntity.getField().contains("Day")){
                    //天数
                    int x = 0;
                    if (!StringUtils.isEmpty(o)){
                        x = Integer.parseInt(o.toString());
                    }
                    int y =  Integer.parseInt(evaluate.toString());
                    cal.put(sdsIndexHeaderEntity.getField(), x + y);
                }else {
                    //占比
                    int x = 0;
                    if (!StringUtils.isEmpty(o)){
                        x = Integer.parseInt(o.toString());
                    }
                    if (Objects.equals(true, evaluate)){
                        cal.put(sdsIndexHeaderEntity.getField(),x+1);
                    } else if (evaluate instanceof Integer) {
                        cal.put(sdsIndexHeaderEntity.getField(),evaluate);
                    } else {
                        cal.put(sdsIndexHeaderEntity.getField(),x);
                    }
                }

                if (!StringUtils.isEmpty(expressionDenStr)){
                    // 使用表达式引擎创建表达式对象
                    Expression expression = engine.createExpression(expressionDenStr);
                    // 使用表达式对象计算
                    Object evaluateDen = expression.evaluate(contextDen);
                    Object obj = calDen.get(sdsIndexHeaderEntity.getField());
                    //费用
                    if (sdsIndexHeaderEntity.getField().contains("Fee")){
                        double x = 0;
                        if (!StringUtils.isEmpty(obj)){
                            x = Double.parseDouble(obj.toString());
                        }
                        double y =  Double.parseDouble(evaluateDen.toString());
                        calDen.put(sdsIndexHeaderEntity.getField(), x + y);
                    }else if (sdsIndexHeaderEntity.getField().contains("Day")){
                        //天数
                        int x = 0;
                        if (!StringUtils.isEmpty(obj)){
                            x = Integer.parseInt(obj.toString());
                        }
                        int y =  Integer.parseInt(evaluateDen.toString());
                        calDen.put(sdsIndexHeaderEntity.getField(), x + y);
                    }else {
                        //占比
                        int x = 0;
                        if (!StringUtils.isEmpty(obj)){
                            x = Integer.parseInt(obj.toString());
                        }
                        if (Objects.equals(true, evaluateDen)){
                            calDen.put(sdsIndexHeaderEntity.getField(),x+1);
                        } else {
                            calDen.put(sdsIndexHeaderEntity.getField(), x);
                        }
                    }
                }
            }
            System.out.println(cn.hutool.core.util.StrUtil.format("inCaseId = {}, 用时：{}ms",inCaseId,System.currentTimeMillis() - start2));
        }
        //处理数据
        for (HashMap.Entry<String, Object> entry : cal.entrySet()){
            String key = entry.getKey();
            Object o = cal.get(key);
            Object objDen = calDen.get(key);
            if (key.contains("Fee")){
                double x = 0;
                double y = 0;
                if (!StringUtils.isEmpty(o)){
                    x = Double.parseDouble(o.toString());
                }
                if (!StringUtils.isEmpty(objDen)){
                    y = Double.parseDouble(objDen.toString());
                    resMap.put(key+"Den", y);
                } else {
                    y = analysisSdsPortalVo.getCount();
                }

                Object val = null;
                if (y == 0) {
                    val = 0;
                } else {
                    val = StrUtil.deciFormatDouble(x, (int) y);
                }
                resMap.put(key, val);
                resMap.put(key+"Num", o);
            }else if (key.contains("Day")){
                int x = 0;
                int y = 0;
                if (!StringUtils.isEmpty(o)){
                    x = Integer.parseInt(o.toString());
                }
                if (!StringUtils.isEmpty(objDen)){
                    y = Integer.parseInt(objDen.toString());
                    resMap.put(key+"Den", y);
                } else {
                    y = analysisSdsPortalVo.getCount();
                }
                Object val = null;
                if (y == 0) {
                    val = 0;
                } else {
                    val = StrUtil.deciFormatU(x, y);
                }
                resMap.put(key, val);
                resMap.put(key+"Num", o);
            } else if (key.contains("Index")) {
                int x = Integer.parseInt(o.toString());
                int y = 0;
                if (!StringUtils.isEmpty(o)){
                    x = Integer.parseInt(o.toString());
                }
                if (!StringUtils.isEmpty(objDen)){
                    y = Integer.parseInt(objDen.toString());
                    resMap.put(key+"Den", y);
                } else {
                    y = analysisSdsPortalVo.getCount();
                }
                Object val = StrUtil.deciFormat(x, y*indexNum);
                resMap.put(key, val);
                resMap.put(key+"Num", x / (double) indexNum);
            }
            else {
                int x = Integer.parseInt(o.toString());
                int y = 0;
                if (!StringUtils.isEmpty(o)){
                    x = Integer.parseInt(o.toString());
                }
                if (!StringUtils.isEmpty(objDen)){
                    y = Integer.parseInt(objDen.toString());
                    resMap.put(key+"Den", y);
                } else {
                    y = analysisSdsPortalVo.getCount();
                }
                Object val = StrUtil.deciFormat(x, y);
                resMap.put(key, val);
                resMap.put(key+"Num", x);
            }
        }
        res.add(resMap);
    }

    public Integer calcTriggerExp(Long mrListId, SdsQcformItemEntity formItem, Map<String, Object> editMap) {
        // 项目全部默认显示
        Integer editPower = 1;
        //1、ScriptEngineManager是JDK6提出的相关方法，这方式的主要目的就是用来对脚本语言的处理。
        //2、ScriptEngineManager是在java的javax.script的包下，是Java原生提供的脚本语言处理方法。
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        if (CommonUtil.isEmpty(formItem.getTriggerExp())) {
            return editPower;
        } else {
            // 带表达式的项目默认不显示，只有表达式条件成立之后才显示
            editPower = 0;
            String triggerExp = formItem.getTriggerExp();
            Long entityId = formItem.getEntityId();
            // 查找规则公式中括号以内的字符
            Pattern p = Pattern.compile(p1);// 查找规则公式中括号以内的字符
            // 替换关联项目的值
            Matcher m = p.matcher(triggerExp);
            while (m.find()) {// 遍历找到的所有括号
                if (m.group().contains("\"")) { // 如果里面是常量，则继续下一个
                    continue;
                }
                // 取得关联项目代码
                String itemCode = m.group().substring(1, m.group().length() - 1);
                // 通过项目代码查询项目id并取值
                String linkVal = "";
                if (editMap.get(itemCode) != null && (Integer) editMap.get(itemCode) == 0) {
                    linkVal = "";
                } else {
                    SdsQcformItemEntity linkItem = FormItemCache.me().getFormItemByCode(entityId, itemCode);

                    SdsQcfitemExecresultEntity linkItemExec = new SdsQcfitemExecresultEntity();
                    linkItemExec.setformItemId(linkItem.getId());
                    linkItemExec.setInCaseId(mrListId);
                    linkItemExec = sdsQcfitemExecresultMapper.selectOne(linkItemExec);
                    // 如果有项目值为空，则默认不符合
                    if (!CommonUtil.isEmpty(linkItemExec)) {
                        linkVal = linkItemExec.getItemResult();
                    }
                }
                // 如果表达式有引号，那么是字符串比较，给项目值加上双引号
//                if (triggerExp.contains("\"")) {
                linkVal = "(\"" + linkVal + "\")";
//                }
                // 将值替换表达式中项目代码
                triggerExp = triggerExp.replace(m.group(), linkVal);
            }
            if (!triggerExp.matches(p3)) {
                String[] expArr = triggerExp.split(":", 2);
                String subExpType_l = expArr[0];
                String subExp_l = expArr[1];

                long divisor = 1;
                if (subExpType_l.contains("CALC")) {
                    switch (subExpType_l) {
                        case "CALC-h":
                            // 1小时毫秒数
                            divisor = 60 * 60 * 1000;
                            break;
                        case "CALC-m":
                            // 一分钟毫秒数
                            divisor = 60 * 1000;
                            break;
                        case "CALC-day":
                            // 一天毫秒数
                            divisor = 24 * 60 * 60 * 1000;
                            break;
                        case "CALC-month":
                            // 一个月毫秒数
                            divisor = 30l * 24l * 60l * 60l * 1000l;
                            break;
                        case "CALC-year":
                            // 一年毫秒数
                            divisor = 12l * 30l * 24l * 60l * 60l * 1000l;
                            break;
                        default:
                            break;
                    }
                    // 查找规则公式中小括号以内的值
                    // 替换关联项目的值
                    m = p.matcher(subExp_l);
                    String execExp = subExp_l;
                    while (m.find()) {
                        if ("()".equals(m.group()) || "(\"\")".equals(m.group())) {
                            execExp = execExp.replace(m.group(), "0");
                            continue;
                        }
                        String val = m.group().substring(1, m.group().length() - 1).replace("\"", "");
                        Long times = DateUtil.getTime(val);
                        int minutes = Math.toIntExact(times / divisor);
                        execExp = execExp.replace(val, String.valueOf(minutes));
                    }
                    triggerExp = triggerExp.replace(subExp_l, execExp);
                }
                triggerExp = triggerExp.replace(subExpType_l, "");
            } else {
                // 查找规则公式中大括号以内的字符
                Pattern pat3 = Pattern.compile(p3);
                // 替换关联项目的值
                Matcher m3 = pat3.matcher(triggerExp);
                while (m3.find()) {
                    // 大括号中间的表达式
                    String subExpStr_l = m3.group().substring(1, m3.group().length() - 1);
                    String[] expArr = subExpStr_l.split(":", 2);
                    String subExpType_l = expArr[0];
                    String subExp_l = expArr[1];

                    long divisor = 1;
                    if (subExpType_l.contains("CALC")) {
                        switch (subExpType_l) {
                            case "CALC-h":
                                // 1小时毫秒数
                                divisor = 60 * 60 * 1000;
                                break;
                            case "CALC-m":
                                // 一分钟毫秒数
                                divisor = 60 * 1000;
                                break;
                            case "CALC-day":
                                // 一天毫秒数
                                divisor = 24 * 60 * 60 * 1000;
                                break;
                            case "CALC-month":
                                // 一个月毫秒数
                                divisor = 30l * 24l * 60l * 60l * 1000l;
                                break;
                            case "CALC-year":
                                // 一年毫秒数
                                divisor = 12l * 30l * 24l * 60l * 60l * 1000l;
                                break;
                            default:
                                break;
                        }
                        // 查找规则公式中小括号以内的值
                        // 替换关联项目的值
                        m = p.matcher(subExp_l);
                        String execExp = subExp_l;
                        while (m.find()) {
                            if ("()".equals(m.group()) || "(\"\")".equals(m.group())) {
                                execExp = execExp.replace(m.group(), "0");
                                continue;
                            }
                            String val = m.group().substring(1, m.group().length() - 1).replace("\"", "");
                            Long times = DateUtil.getTime(val);
                            int minutes = Math.toIntExact(times / divisor);
                            execExp = execExp.replace(val, String.valueOf(minutes));
                        }
                        triggerExp = triggerExp.replace(subExp_l, execExp);
                    }
                    triggerExp = triggerExp.replace(subExpStr_l, subExp_l);
                    triggerExp = triggerExp.replace(subExpType_l, "");
                }
            }
            triggerExp = triggerExp.replace(":", "");
            if (triggerExp.contains("contains")) {
                triggerExp = modifyExpression(triggerExp);
            }
            // 将表达式中的大括号中括号替换为小括号，以及其他其他字符替换为可执行字符
            triggerExp = triggerExp.replace("{", "(").replace("}", ")");
            triggerExp = triggerExp.replace("[", "(").replace("]", ")");
            triggerExp = triggerExp.replace("contains(\"\")", "contains(\",,\")");
            triggerExp = triggerExp.replace("oth", "OTH").replace("def", "DEF");

            // 运算前置条件表达式
            try {
                if ((boolean) engine.eval(triggerExp)) {
                    editPower = 1;
                }
            } catch (ScriptException e) {
                // 表达式解析失败，默认返回1
                return 1;
            }
        }
        return editPower;
    }


    public String modifyExpression(String expression) {
        String aExp = expression;

        //判断表达式中有无中括号
        if (!expression.matches(p2)) {
            if (!expression.contains("contains")) {
                return aExp;
            }
            // 循环处理原始表达式中的子表达式
            String[] subStrArr = aExp.split(".contains");
            for (int ExpInd = 0; ExpInd < subStrArr.length; ExpInd++) {
                String expSub = subStrArr[ExpInd];  // 提取子表达式
                // 提取引号内的内容
                String regExp= "(\").*?(\")";
                Pattern pattern = Pattern.compile(regExp);
                Matcher matcher = pattern.matcher(expSub);
                while (matcher.find()) {
                    String group = matcher.group();
                    String result = group.substring(1, group.length() - 1);
                    result = "\"," + result + ",\"";
                    aExp = aExp.replaceFirst(group, result);
                }
            }
        } else {
            // 查找规则公式中中括号以内的字符
            Pattern pat2 = Pattern.compile(p2);
            // 替换关联项目的值
            Matcher m2 = pat2.matcher(aExp);
            while (m2.find()) {
                String subGroup = m2.group();
                if (!subGroup.contains("contains")) {
                    continue;
                }
                String tmpGroup = subGroup;
                String tmpSubFroup = subGroup.substring(1, subGroup.length() - 1);
                // 循环处理原始表达式中的子表达式
                String[] subStrArr = tmpSubFroup.split(".contains");
                for (int ExpInd = 0; ExpInd < subStrArr.length; ExpInd++) {
                    String expSub = subStrArr[ExpInd];  // 提取子表达式
                    // 提取引号内的内容
                    String regExp= "(\").*?(\")";
                    Pattern pattern = Pattern.compile(regExp);
                    Matcher matcher = pattern.matcher(expSub);
                    while (matcher.find()) {
                        String group = matcher.group();
                        String result = group.substring(1, group.length() - 1);
                        result = "\"," + result + ",\"";
                        tmpGroup = tmpGroup.replaceFirst(group, result);
                    }
                }
                aExp = aExp.replace(subGroup, tmpGroup);
            }
        }

        return aExp;
    }

}
